覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

WSL環境へのPostgreSQLのインストール作業メモ

Windows 11のWSL(Ubuntu)環境に PostgreSQL をインストールした際の実行メモです。


■まずは sudo apt update で パッケージ一覧を更新

sudo apt update


■Postgres をインストール

sudo apt install postgresql


■Postgres の再起動を実行

sudo /etc/init.d/postgresql restart


■postgres ユーザにチェンジ

sudo -u postgres -i


■DB用のユーザ作成

createuser -d -U postgres -P db-user01


■作成したDB用のユーザをownerとする新規DBの作成

createdb db-001 --encoding=UTF-8 --owner=db-user01


■postgres ユーザから通常のユーザに戻って、psqlを使用してDBに接続できることを確認

psql -U db-user01 -h localhost -d db-001


全体的な実行のイメージは以下の通りです

ykiv@my-pc:/mnt/c/Users/ykiv$ sudo apt update
・・・
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
...
Get:44 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 c-n-f Metadata [804 B]
Get:45 http://archive.ubuntu.com/ubuntu focal-backports/multiverse amd64 c-n-f Metadata [116 B]
...
ykiv@my-pc:/mnt/c/Users/ykiv$
ykiv@my-pc:/mnt/c/Users/ykiv$ sudo apt install postgresql
...
Success. You can now start the database server using:

    pg_ctlcluster 12 main start

Ver Cluster Port Status Owner    Data directory              Log file
12  main    5432 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
update-alternatives: using /usr/share/postgresql/12/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
invoke-rc.d: could not determine current runlevel
Setting up sysstat (12.2.0-2ubuntu0.1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up postgresql (12+214ubuntu0.1) ...
Processing triggers for systemd (245.4-4ubuntu3) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...
ykiv@my-pc:/mnt/c/Users/ykiv$
ykiv@my-pc:/mnt/c/Users/ykiv$ sudo /etc/init.d/postgresql restart
 * Restarting PostgreSQL 12 database server                                                                      [ OK ]
ykiv@my-pc:/mnt/c/Users/ykiv$
ykiv@my-pc:/mnt/c/Users/ykiv$ sudo -u postgres -i
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.60.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
...
This message is shown once once a day. To disable it please create the
/var/lib/postgresql/.hushlogin file.
postgres@my-pc:~$
postgres@my-pc:~$ createuser -d -U postgres -P db-user01
Enter password for new role:
Enter it again:
postgres@my-pc:~$
postgres@my-pc:~$ createdb db-001 --encoding=UTF-8 --owner=db-user01
postgres@my-pc:~$ logout
ykiv@my-pc:/mnt/c/Users/ykiv$
ykiv@my-pc:/mnt/c/Users/ykiv$ psql -U db-user01 -h localhost -d db-001
Password for user db-user01:
psql (12.9 (Ubuntu 12.9-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
db-001=> \q
ykiv@my-pc:/mnt/c/Users/ykiv$


Postgresユーザのパスワードを変更

postgres ユーザの DB接続パスワードを "postgres" に変更する。

sudo -u postgres psql で psql 実行して ALTER ROLE postgres WITH PASSWORD でパスワードを変更

ykiv@my-pc:/mnt/c/Users/ykiv$ sudo -u postgres psql
psql (12.9 (Ubuntu 12.9-0ubuntu0.20.04.1))
Type "help" for help.

postgres=# ALTER ROLE postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=# 
postgres=# \q
ykiv@my-pc:/mnt/c/Users/ykiv$



関連エントリ