ポイントは「yum module list」等のyumモジュールと「peer認証」かと思います。
install
まずは、postgresqlのモジュール一覧 確認
$ sudo yum module list postgresql CentOS-8 - AppStream Name Stream Profiles Summary postgresql 9.6 client, server [d] PostgreSQL server and client module postgresql 10 [d][e] client, server [d] PostgreSQL server and client module postgresql 12 client, server [d] PostgreSQL server and client module Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
上記のように、defaultでは、postgrs10の為、一旦、resetし、install
$ sudo yum install @postgresql:12/server Dependencies resolved. The operation would result in switching of module 'postgresql' stream '10' to stream '12' Error: It is not possible to switch enabled streams of a module. It is recommended to remove all installed content from the module, and reset the module using 'yum module reset <module_name>' command. After you reset the module, you can install the other stream. $ sudo yum module reset postgresql $ sudo yum install @postgresql:12/server $ sudo yum install @postgresql:12/client
initdb や postgres の起動
$ sudo /usr/bin/postgresql-setup --initdb * Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log $ sudo systemctl enable postgresql $ sudo systemctl start postgresql $ /usr/bin/psql --version psql (PostgreSQL) 12.5
defaultの peer 認証を trustへ変更
defaultでは、"su - postgres" した上で、 "/usr/bin/psql -U postgres"する必要がある為。
$ /usr/bin/psql -U postgres psql: error: FATAL: Peer authentication failed for user "postgres" $ sudo su - # pwd /var/lib/pgsql/data # cp pg_hba.conf pg_hba.conf.20210131 # vi pg_hba.conf #TYPE DATABASE USER ADDRESS METHOD old) local all all peer new) local all all trust $ /usr/bin/psql -U postgres psql (12.5) Type "help" for help. postgres=#
ユーザ(ロール)追加
$ /usr/bin/psql -U postgres postgres=# CREATE ROLE testuser LOGIN PASSWORD 'testuser'; postgres=# ALTER ROLE testuser WITH CREATEDB CREATEROLE LOGIN; postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- testuser | Create role, Create DB | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
PASSWORDロールも追加すると、psqlによるログイン時にパスワード入力を求めれるようです。 (試していませんが)
データベース追加 (create database)
先程の追加ユーザをオーナとする為、以下のように行います。
$ /usr/bin/psql -U testuser postgres postgres=> CREATE DATABASE db_rear_hon ENCODING='EUC_JP' LC_COLLATE='C' LC_CTYPE='C' TEMPLATE='template0'; postgres=> \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -------------+----------+----------+-------------+-------------+----------------------- db_rear_hon | testuser | EUC_JP | C | C | 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