PostgreSQL

Last-modified: 2020-01-28 (火) 17:25:07

インストール(CentOS7)

# yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum -y install postgresql12-server

DB初期化

# PGSETUP_INITDB_OPTIONS="-E UTF8 --locale=C" /usr/pgsql-12/bin/postgresql-12-setup initdb

サービス起動、登録

# systemctl start postgresql-12.service
# systemctl enable postgresql-12.service

データベース、テーブルの作成

# su - postgres
$ psql
postgres=# CREATE DATABASE testdb;
postgres=# \q
$ psql -d testdb
testdb=# CREATE TABLE・・・

外部から接続できるように

# vi /var/lib/pgsql/12/data/postgresql.conf
#listen_addresses = 'localhost'
↓
listen_addresses = '*'

認証方式の変更(ユーザ・パスワード指定でログインできるように)

OSのpostgresユーザのパスワードを設定

# passwd postgres

PostgreSQLのpostgresユーザのパスワードを設定

# psql
# alter role postgres with password 'postgres';

PostgreSQLの設定変更

# vi /var/lib/pgsql/12/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

※ 認証方式を「md5」に変更。

サービス再起動

systemctl restart postgresql-12.service

ユーザ・パスワード指定でログイン

# PGPASSWORD=postgres psql -d testdb -U postgres