Ubuntu 20.04 に PostgreSQL と pgAdmin4 をインストールする

 
よく使うのでメモ
 

PostgreSQL のインストール

 
まずは apt を更新しておいて、それから postgreSQL をインストール。

sudo apt update -y
sudo apt install -y postgresql postgresql-contrib

これでデフォルトのユーザーとデータベースが出来上がってるのでそれを使う。

sudo su - postgres

postgres ユーザーに su したら psqlコマンドを実行してパスワードを変更する。
変更後は quit で終了。

-- psql
ALTER USER postgres WITH PASSWORD 'NEW_PASSWORD';
-- quit

あとはデフォルトDBじゃなくて別に作りたいので、別ユーザーと一緒に作っておく。

create database testdb;
create user testuser with encrypted password 'NEWPASS';
grant all privileges on database testdb to testuser;
alter role testuser with superuser;

そして peer 認証じゃなくてパスワード認証にするために pg_hba.conf を修正
リモートアクセスも許可したいのでホストを追加

# vim /etc/postgresql/12/main/pg_hba.conf
#local   all             postgres                               peer #コメントアウト
local   all             all                               md5
host    all             all          10.0.0.0/24          md5 #LAN のIPアドレスレンジに合わせて変更

あとは postgresql.conf も修正

# vim /etc/postgresql/12/main/postgresql.conf
listen_addresses = '*' #追加

反映させるために systemctl で postgresql サービスを restart する。

あとは管理ツールのパスを通しておく。
.profile変更後は source コマンドで反映。

# vim ~/.profile
export PATH=$PATH:/usr/lib/postgresql/12/bin
# source ~/.profile

 

SSL 設定 (2022/05/09追記)

LetsEncrypt で証明書取得してるので、その証明書を使う設定。
/etc/letsencrypt/renewal-hooks/deploy/postgresql.deploy ファイルを用意して、証明書更新時にpostgresにもコピーして使う。

#!/bin/bash
# /etc/letsencrypt/renewal-hooks/deploy/postgresql.deploy
umask 0177
DOMAIN=psql.example.com
DATA_DIR=/var/lib/postgresql/12/main
cp /etc/letsencrypt/live/$DOMAIN/fullchain.pem $DATA_DIR/server.crt
cp /etc/letsencrypt/live/$DOMAIN/privkey.pem $DATA_DIR/server.key
chown postgres:postgres $DATA_DIR/server.crt $DATA_DIR/server.key
systemctl restart postgresql

ファイルを作ったら、chmod で実行権を付与する。

sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/postgresql.deploy 

あとは postgresql.conf も修正。

# /etc/postgresql/12/main/postgresql.conf 
ssl = on  
ssl_cert_file = 'server.crt'  
ssl_key_file = 'server.key'  
ssl_prefer_server_ciphers = on

pg_hba.conf も修正。 hostssl のエントリを追加する。

# /etc/postgresql/12/main/pg_hba.conf 
host    all     all     0.0.0.0/0       md5
hostssl all     all     0.0.0.0/0       md5

ここまでやったら systemctl で postgresql を再起動。

接続クライアント側の設定を sslmode=require にすれば SSL 接続になる

psql -d "dbname=testdb sslmode=require" -h psql.example.com -U testuser
Password for user testuser: 
psql (12.10 (Ubuntu 12.10-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

testdb=# 

参考サイト
Use SSL Encryption with PostgreSQL on Ubuntu 20.04 – Vultr.com

 

pgAdmin4 のインストール

 
apt のリポジトリを追加してからインストール

curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
sudo apt install -y pgadmin4

インストールが終わったらセットアップスクリプトを実行

sudo /usr/pgadmin4/bin/setup-web.sh

ログイン用のメアドを入力してパスワードを作成したらセットアップ完了。
これで http://localhost/pgadmin4 にアクセス出来る。

 
おまけ)

WSL2 だと setup-web.sh の最後でエラーが出るが、これは systemctl で apache2 の再起動が出来ないため。
代わりに service で再起動してあげれば良い。

sudo service apache2 restart