schedule2019-05-14

Amazon Linux2にPostgreSQL10をインストールして初期設定する

Amazon Linux2にPostgreSQL10をインストールして初期設定するまでの手順です。 yumリポジトリにはv10があるので楽に導入できました。

2019-06-26に2回目のインストールを行いました。 この手順で大丈夫そうです。

環境

  • Amazon Linux2
  • postgresql 10.4
  • postgresql-server 10.4

インストールと立ち上げ手順

info

まずはインストールするパッケージの確認をする。 yum infoの情報を載せた(一部省略)。

$ sudo yum info postgresql-server postgresql

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : postgresql
Arch        : x86_64
Version     : 10.4
Release     : 5.amzn2.0.2
Size        : 5.2 M
Repo        : installed
From repo   : amzn2extra-postgresql10
Summary     : PostgreSQL client programs


Available Packages
Name        : postgresql-server
Arch        : x86_64
Version     : 10.4
Release     : 5.amzn2.0.2
Size        : 4.9 M
Repo        : amzn2extra-postgresql10/2/x86_64
Summary     : The programs needed to create and run a PostgreSQL server

yumリポジトリにPostgreSQLのRepository Packagesを導入が必要かと思ったが、amzn2extra-postgresql10に入っているようです。

install

インストールとバージョンの確認は以下のコマンドでOK。

$ sudo yum install -y postgresql-server postgresql
$ psql -V
psql (PostgreSQL) 10.4

起動と自動起動

現時点ではpostgresqlが立ち上がっていないため、操作しようとすると下記のようなエラーが出る。

$ psql -l
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

postgresqlを立ち上げ、自動起動させます。

# 自動起動
$ sudo systemctl enable postgresql

# 起動
$ sudo systemctl start postgresql

# ステータス確認
$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-05-14 14:41:05 JST; 1s ago
  Process: 13572 ExecStartPre=/usr/libexec/postgresql-check-db-dir %N (code=exited, status=0/SUCCESS)
 Main PID: 13574 (postmaster)
   CGroup: /system.slice/postgresql.service
           ├─13574 /usr/bin/postmaster -D /var/lib/pgsql/data
           ├─13577 postgres: logger process
           ├─13579 postgres: checkpointer process
           ├─13580 postgres: writer process
           ├─13581 postgres: wal writer process
           ├─13582 postgres: autovacuum launcher process
           ├─13583 postgres: stats collector process
           └─13584 postgres: bgworker: logical replication launcher

停止する際は、sudo systemctl stop postgresqlでOKです。

立ち上がらないとき

$ sudo systemctl start postgresql
Job for postgresql.service failed because the control process exited with error code.
See "systemctl status postgresql.service" and "journalctl -xe" for details.

DBの初期化が必要だった

$ sudo postgresql-setup --initdb

初期設定

psqlで操作しようとした場合下記のようなエラー出ます。

$ psql -l
psql: FATAL:  role "ec2-user" does not exist

これはpg_hba.confの初期設定がpeerになっているためです。 一応、実行ユーザーのpostgresで行うと回避できます。

[ec2-user@hostname ~]$ sudo -u postgres psql -l

初期パスワード

ユーザpostgresも初期パスワードの設定が必要です。

$ sudo passwd postgres
Changing password for user postgres.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

postgresql.conf

/var/lib/pgsql/data/に設定ファイルが入っています。 postgresql.confでは外部に公開するためIPアドレスを変更しました。

# オリジナルを残して
$ sudo cp /var/lib/pgsql/data/postgresql.conf /var/lib/pgsql/data/postgresql.conf.org
# 編集する
$ sudo vi /var/lib/pgsql/data/postgresql.conf
-- #listen_addresses = 'localhost'          # what IP address(es) to listen on;
++ listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
#port = 5432                            # (change requires restart)
max_connections = 100                   # (change requires restart)

pg_hba.conf

pg_hba.confではロールを設定します。

$ sudo cp /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.org
$ sudo vi /var/lib/pgsql/data/pg_hba.conf

ローカルからは認証をなくし、外部からはパスワードが必要になるよう変更します。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
-- local   all             all                                     peer
++ local   all             all                                     trust
# IPv4 local connections:
-- host    all             all             127.0.0.1/32            ident
++ host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
-- host    all             all             ::1/128                 ident
++ #host    all             all             ::1/128                 ident

認証は以下の方式です。

設定が済んだら再起動します。

# 再起動
$ sudo systemctl restart postgresql

以上でPostgreSQL10のインストールと初期設定が終わりです。

# psqlに接続
$ psql -U postgres