LoginSignup
18
15

More than 5 years have passed since last update.

[Ubuntu 16.04] PostgreSQLのリモート接続設定

Last updated at Posted at 2017-05-04

Ubuntuサーバ上にPostgreSQLをインストールしてリモート接続設定する機会があったので再設定時のためにメモ.

postgresql インストール

まずターミナルからPostgreSQLをインストールする.

sudo apt-get update
sudo apt-get install postgresql 

Ubuntu16.04上ではインストールされたPostgreSQLは /etc/postgresql に展開される.
上記のディレクトリに移動して設定ファイルを確認する.

cd /etc/postgresql/[Version]/main

ls
postgresql.conf pg_hba.conf ... etc

今回設定で使用するファイルはpostgresql.confとpg_hba.confの2つである.まずpg_hba.confに関しての以下の通り設定を行う.

pg_hba.confの修正

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

上記のような設定箇所があるのでこれのIPv4コネクションの箇所を修正する.初期状態ではローカルのみからアクセス許可になっているのでこれに以下のように追記する.

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
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
...

ipアドレスを0.0.0.0/0とすることで全ipからのパスワード接続を許可することが可能となる.自分のプライベートネットワークipを把握してる場合には192.168.**.0/24 (**は自分で調べる)のようにして制限すると良いと思う.
またmethodについてはパスワード認証であるmd5を指定しているが,この場合には対象データベースのみにアクセスできるユーザの利用ができないらしい..使ったことないので分からない.
この場合にはmethodを通常のパスワード認証であるpasswordにしてSSL経由でアクセスが勧められているらしい.(今回は採用していない)
余裕がある場合にはrootユーザであるpostgresでのリモート接続を拒否した方が良いと思うので下記のようにするとベスト.

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
host    all             all             0.0.0.0/0               md5
host    all             postgres        0.0.0.0/0               reject
# IPv6 local connections:
host    all             all             ::1/128                 md5
...

今回はやってないがローカルでrootユーザからのアクセスもpeer認証に変更した方が良い.
これで一度ローカルのターミナル上でrootユーザにログインしてからでないと,rootユーザ経由でのデータベースアクセスをできないように制限できる.
ちなみにmethodをtrustにするとパスワード認証すらなくなるので宜しくない.
これでpg_hba.confの設定を終了して,次にpostgresql.confに関しての設定をする.

postgresql.confの修正

postgresql.conf
...

#listen_addresses = 'localhost'

...

上記の箇所がファイル内に存在するのでこれをコメントアウトを解除して以下のように設定することで,リモート接続を許可する.

postgresql.conf
...

listen_addresses = '*'

...

以上の設定が終了後にPostgreSQLを再起動すればOK.

postgresqlに関して

ちなみに起動・停止・確認・再起動のコマンドは以下の通り.

/etc/init.d/postgresql start     ## 起動

/etc/init.d/postgresql stop      ## 停止

/etc/init.d/postgresql status    ## 確認

/etc/init.d/postgresql restart   ## 再起動

最後に,ファイアウォール等を設定している場合はPostgreSQLのデフォルトポートである5432は開ける作業も必要.

18
15
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
18
15