CentOS Stream 8
Sponsored Link

PostgreSQL 10 : インストール2021/05/27

 
PostgreSQL をインストールし、データベースサーバーを構築します。
[1] PostgreSQL をインストールして起動します。
[root@www ~]#
dnf module -y install postgresql:10
[root@www ~]#
postgresql-setup --initdb

 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log

[root@www ~]#
systemctl enable --now postgresql

[2] デフォルト設定では、ローカルホストからのみ接続可能 且つ ローカル接続は [peer] 認証のみとなっています。 認証方式の詳細は公式ドキュメントを参照ください。
⇒ https://www.postgresql.jp/document/10/html/auth-pg-hba-conf.html
# ローカルホストをリスン

[root@www ~]#
grep listen_addresses /var/lib/pgsql/data/postgresql.conf

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

[root@www ~]#
grep -v -E "^#|^$" /var/lib/pgsql/data/pg_hba.conf

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

[root@www ~]#
useradd cent
# PostgreSQL 管理ユーザーで PostgreSQL ユーザーとデータベース追加

[root@www ~]#
su - postgres

[postgres@www ~]$
createuser cent

[postgres@www ~]$
createdb testdb -O cent
# 確認

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

[cent@www ~]$
psql testdb

psql (10.6)
Type "help" for help.

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

# データベース一覧を表示
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    | cent     | 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 | cent
(1 row)

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

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

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

testdb=> \dt 
Did not find any relations.

# exit する
testdb=> \q 

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

[cent@www ~]$
dropdb testdb

[cent@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
(3 rows)
関連コンテンツ