0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【検証】PostgreSQLをインストールする

Posted at

今回はGitLabサーバーで利用するDBサーバー(PostgreSQL)を別のサーバーから参照するために、その準備としてPostgreSQLをインストールします。

PostgreSQL インストール

まずは以下のコマンドを実行し、パッケージの取得を行います。

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

続いて以下のコマンドでPostgreSQLサーバーをインストールします。

yum -y install postgresqlXX-server #XXにはバージョンを指定(最新verは13の模様)
※以降、バージョン13として記載する

インストール完了後、以下コマンドで初回起動を実行します。
注意点として、root等の特権ユーザーでは実行出来ないため、コマンド実行前にpostgresユーザーにスイッチします。

su postgres
/usr/pgsql-13/bin/initdb -D <データベースのディレクトリ> #データベースの格納先のデフォルトは「/var/lib/pgsql/13/data/」
※ちなみに過去のバージョンだとデータベースの格納ディレクトリは/var/lib/pgsql/data/だったようです

上記コマンド実行後、Successと表示されれば成功です。
続いて以下のコマンドでサービスの起動を行います。

systemctl start postgresql-13.service

サービス起動後、psql -lコマンドを実行して以下のような結果が出力されれば実行出来ています。

psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileg
es
-----------+----------+----------+-------------+-------------+------------------
-----
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres
    +
           |          |          |             |             | postgres=CTc/post
gres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres
    +
           |          |          |             |             | postgres=CTc/post
gres
(3 rows)

PostgreSQL 色々な設定

インストール完了後、デフォルトで作成されるpostgresユーザーのパスワードを設定します。
パスワードを変更するために一度rootまたはroot権限のあるユーザーへスイッチします。

su -
passwd postgres
新しいパスワードを設定する

続いて、外部からアクセスするために最低限必要なセキュリティ設定を行います。
内容としては、特定のアドレスからパスワード認証でアクセスするというものです。

vi /var/lib/pgsql/13/data/pg_hba.conf
※最下行に以下の形式で追加する
host    all             all             <クライアントネットワークアドレス>/<プレフィックス長>           md5

次にPostgreSQLに接続を許可するため、PostgreSQLのポート番号5432でリッスンするアドレスを変更します。
/var/lib/pgsql/13/data/postgresql.confファイルのlisten_addressesの値を変更します。
デフォルトではコメントアウトされているので、listen_addressesの行の先頭の#を外します。

vi /var/lib/pgsql/13/data/postgresql.conf
※listen_addressesの行を検索し、「localhost」から「*」に変更する
listen_addresses = '*'

次に接続テスト用のDB作成と、postgresユーザーのパスワードを設定します。

createdb <DB名>
psql -d <DB名>
alter user postgres with password '<設定したいパスワード>';
\q

設定が完了しましたら、systemctl restart postgresql-13.serviceを実行します。
次にPostgreSQLに外部サーバーからアクセスするためにFirewallを許可します。

firewall-cmd --permanent --add-port=5432/tcp
firewall-cmd --reload

上記設定完了後、クライアントにて以下のコマンドを実行して接続テストします。

psql -d <DB名> -h <ホスト名/IPアドレス> -p 5432 -U postgres
ユーザ postgres のパスワード:<設定したパスワード>
※「<DB名>=#」といったプロンプトが返ってくれば接続成功

以上で今回の検証兼事前準備は終了です。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?