Since you can't mess with the production environment, there are times when you want to prepare a specific version of the DB environment in another environment. It is a memo for myself in such a case.
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt update
apt install postgresql-9.5
systemctl start [email protected]
su postgres
psql -U
# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
create database test
Set the pkey column as the primary key.
create table test (pkey serial primary key, id integer);
# \d
List of relations
Schema | Name | Type | Owner
--------+---------------+----------+----------
public | test | table | postgres
public | test_pkey_seq | sequence | postgres
(2 rows)
When postgreSQL sets the primary key, the "test_pkey_seq" table is automatically created.
postgres=# \c test
You are now connected to database "test" as user "postgres".
# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------------------------------------------------
pkey | integer | not null default nextval('test_pkey_seq'::regclass)
id | integer |
Indexes:
"test_pkey" PRIMARY KEY, btree (pkey)
insert into test (id) values ('5');
# select * from test;
pkey | id
------+----
1 | 5
(4 rows)
pkey is automatic.
# alter table test add column id2 integer;
I can't connect to postgreSQL from another console even though the command is correct.
psql -U postgres test
psql: FATAL: Peer authentication failed for user "postgres"
cd /etc/postgresql/9.5/main
vim pg_hba.conf
pg_nba.Before changing conf
local all postgres peer
pg_nba.After changing conf
local all postgres md5
Restart postgreSQL.
systemctrl restart postgresql
I was connected at another terminal. OK OK.
Recommended Posts