LoginSignup
3
3

More than 5 years have passed since last update.

CentOS7.5にPostgreSQL10をインストールする方法

Posted at

CentOS7.5にPostgreSQL10をインストール、初期設定するまでのやり方を紹介します。

PostgreSQL10をインストールする

リポジトリを追加

始めにPostgreSQL10のリポジトリを追加します。

$ sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

他のバージョンを追加したい場合はこちらからリポジトリのURLを確認できます。

PostgreSQL: Linux downloads (Red Hat family)

インストール

下記コマンドを実行します。

$ sudo yum install -y --enablerepo=pgdg10 postgresql10 postgresql10-server
# 確認
$ psql --version
psql (PostgreSQL) 10.5

インストールが完了したら起動しましょう。

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

失敗しましたね。。。

原因を調べてみると

$ sudo tail -n 50 /var/log/message
Sep  3 11:54:48 192-0-2-58  systemd: Starting PostgreSQL 10 database server...
Sep  3 11:54:48 192-0-2-58 postgresql-10-check-db-dir: "/var/lib/pgsql/10/data/" is missing or empty.
Sep  3 11:54:48 192-0-2-58 postgresql-10-check-db-dir: Use "/usr/pgsql-10/bin/postgresql-10-setup initdb" to initialize the database cluster.
Sep  3 11:54:48 192-0-2-58 postgresql-10-check-db-dir: See /usr/share/doc/postgresql10-10.5/README.rpm-dist for more information.
Sep  3 11:54:48 192-0-2-58 systemd: postgresql-10.service: control process exited, code=exited status=1
Sep  3 11:54:48 192-0-2-58 systemd: Failed to start PostgreSQL 10 database server.
Sep  3 11:54:48 192-0-2-58 systemd: Unit postgresql-10.service entered failed state.
Sep  3 11:54:48 192-0-2-58 systemd: postgresql-10.service failed.

DBクラスタを初期化しろってことなので、下記コマンドを実行しリトライします。

$ sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK

# リトライ
$ sudo systemctl start postgresql-10

無事に起動しました。

初期設定

パスワード設定

まずpostgresユーザのパスワードを設定します。

# インストール時に追加されているpostgresユーザに切り替え
$ sudo su - postgres

# PostgreSQLに接続
-bash-4.2$ psql
psql (10.5)
Type "help" for help.

# パスワード変更
postgres=\# alter role postgres with password 'パスワード';
ALTER ROLE

# 接続を解除
postgres=# \q
-bash-4.2$ exit
logout

アクセス設定

各ユーザにパスワード認証でログインできるよう設定を変更します。

# rootユーザに切り替え
$ sudo su -

# 設定ファイルを編集
$ vim /var/lib/pgsql/10/data/pg_hba.conf
# 下記のように変更 
local   all             all                                     password
# IPv4 local connections:
host    all             all             127.0.0.1/32            password
# IPv6 local connections:
host    all             all             ::1/128                 password

# 再起動
$ sudo systemctl restart postgresql-10

パスワード認証でログインできることを確認します。

$ psql -U postgres
Password for user postgres: # 設定したパスワードを入力
psql (10.5)
Type "help" for help.

postgres=#

無事にパスワードが通って接続できましたね。

自動起動

サーバ起動時にPostgreSQLが自動起動するように設定します。

$ sudo systemctl enable postgresql-10
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3