Ubuntu 20.04
Sponsored Link

PostgreSQL 12 : インストール2020/09/01

 
PostgreSQL をインストールして、データベースサーバーを構築します。
[1] PostgreSQL をインストールして起動します。
root@dlp:~#
apt -y install postgresql-12
[2] デフォルト設定では、ローカルホストからのみ接続可能 且つ ローカル接続は [peer] 認証のみとなっています。 認証方式の詳細は公式ドキュメントを参照ください。
⇒ https://www.postgresql.jp/document/10/html/auth-pg-hba-conf.html
# ローカルホストをリスン

root@dlp:~#
grep listen_addresses /etc/postgresql/12/main/postgresql.conf

#listen_addresses = 'localhost' # what IP address(es) to listen on;
# 認証方式

root@dlp:~#
grep -v -E "^#|^$" /etc/postgresql/12/main/pg_hba.conf

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
[3] [peer] 認証の場合、任意の PostgreSQL ユーザーを追加して利用するには、同名の OS ユーザーも必要になります。
# OS ユーザー追加

root@dlp:~#
adduser ubuntu
# PostgreSQL 管理ユーザーで PostgreSQL ユーザーとデータベース追加

root@dlp:~#
su - postgres

postgres@dlp:~$
createuser ubuntu

postgres@dlp:~$
createdb testdb -O ubuntu
# 確認

postgres@dlp:~$
psql -c "select usename from pg_user;"
 usename
----------
 postgres
 ubuntu
(2 rows)

[postgres@www ~]$
psql -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
 testdb    | ubuntu   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
[4] 新規追加したユーザーで PostgreSQL を利用する場合の基本操作です。
# テストDBに接続

ubuntu@dlp:~$
psql testdb

psql (12.4 (Ubuntu 12.4-0ubuntu0.20.04.1))
Type "help" for help.

# ユーザーロール一覧を表示
testdb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 ubuntu    |                                                            | {}

# データベース一覧を表示
testdb=> \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
 testdb    | ubuntu   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)

# テストテーブルを作成
testdb=> create table test_table (no int, name text); 
CREATE TABLE

# テーブル一覧を表示
testdb=> \dt 
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+--------
 public | test_table | table | ubuntu
(1 row)

# テストテーブルにテストデータを挿入
testdb=> insert into test_table (no,name) values (01,'Ubuntu'); 
INSERT 0 1

# 確認
testdb=> select * from test_table; 
 no |  name
----+--------
  1 | Ubuntu
(1 row)

# テストテーブルを削除
testdb=> drop table test_table; 
DROP TABLE

testdb=> \dt 
Did not find any relations.

# exit する
testdb=> \q 

# テストデータベースを削除

ubuntu@dlp:~$
dropdb testdb

ubuntu@dlp:~$
psql -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
(3 rows)
関連コンテンツ