LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

Organization

Ubuntu 20.04 LTS へ do-release-upgrade した後に Postgresql 10から12にアップグレードした話

毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。

0. Ubuntu で do-relesae-upgradeしました。

そうすると、Ubuntu 18.04 LTSではPostgreSQLが10.15だったのですが、12.5にバージョンアップしました。違うポート番号で両方動くので、そのままでもいいのですがどうせなので、12にデータを移行してアップグレードすることにしました。

Ubuntuの場合はもっと簡単なアップグレード方法があったのでそれについて書きました。

1. データをダンプする。

まず、現在デフォルトで動いている10.15のデータをダンプします。

まず、su postgresでpostgresユーザーになってから、
pg_dumpall でダンプします。
ダンプファイルは、/tmp/postgres10-all.dumpに保存してみましょう。

su postgres
postgres:/tmp$ id
uid=124(postgres) gid=129(postgres) groups=129(postgres),110(ssl-cert)
postgres:/tmp$ pg_dumpall -f /tmp/postgres10-all.dump
postgres:/tmp$ ls -la tmp/
-rw-rw-r--  1 postgres postgres 3709 12月  6 11:57 postgres10-all.dump

2. PostgreSQL 10.12を止める

次に現在デフォルトで動いている10.15を止めます。

$ systemctl start postgresql
postgresql.service          postgresql@10-main.service  postgresql@12-main.service

サービスとしては3つ登録されているようですが、postgresql@10-main.serviceを止めましょう。

sudo systemctl stop postgresql@10-main.service
Stopping postgresql (via systemctl): postgresql.service.

3. PostgreSQL 12.5をメインで動かすように設定する

12.5をメインで動かすためには、いくつか作業が必要です。

  1. pg_hba.confを設定
  2. postgresql.confを設定

この辺りは通常のPostgreSQLの利用設定なので割愛します。
注意が必要なのは、10.15と12.5を共存させるために、postgresql.confのportがデフォルトとは違っているという点です。

$ diff /etc/postgresql/12/main/postgresql.conf{.orig,} -C 1
*** /etc/postgresql/12/main/postgresql.conf.orig	2020-12-06 12:25:37.731048837 +0900
--- /etc/postgresql/12/main/postgresql.conf	2020-12-06 12:26:04.507015568 +0900
***************
*** 58,59 ****
--- 58,60 ----

+ listen_addresses = '*'
  #listen_addresses = 'localhost'		# what IP address(es) to listen on;
***************
*** 62,64 ****
  					# (change requires restart)
! port = 5433				# (change requires restart)
  max_connections = 100			# (change requires restart)
--- 63,65 ----
  					# (change requires restart)
! port = 5432				# (change requires restart)
  max_connections = 100			# (change requires restart)

設定が終わったら、PostgreSQL 12を起動します。

sudo systemctl start postgresql@12-main.service
systemctl status postgresql@12-main.service
● postgresql@12-main.service - PostgreSQL Cluster 12-main
     Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
     Active: active (running) since Sun 2020-12-06 12:15:53 JST; 7min ago
    Process: 2333 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 12-main start (code=exited, status=0/SUCCES>
   Main PID: 2346 (postgres)
      Tasks: 7 (limit: 37802)
     Memory: 21.1M
     CGroup: /system.slice/system-postgresql.slice/postgresql@12-main.service
             ├─2346 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12>
             ├─2348 postgres: 12/main: checkpointer
             ├─2349 postgres: 12/main: background writer
             ├─2350 postgres: 12/main: walwriter
             ├─2351 postgres: 12/main: autovacuum launcher
             ├─2352 postgres: 12/main: stats collector
             └─2353 postgres: 12/main: logical replication launcher

4. データをリストアする

psql -d postgres -f /tmp/postgres10-all.dumpコマンドでデータベースをリストアします。

psql -d postgres -f /tmp/postgres10-all.dump
SET
SET
SET
CREATE ROLE
ALTER ROLE
psql:/tmp/postgres10-all.dump:16: ERROR:  role "postgres" already exists
ALTER ROLE
CREATE DATABASE
GRANT
REVOKE
GRANT
You are now connected to database "k3s_db_01" as user "postgres".
SET
SET
SET
SET
SET
SET
 set_config 
------------
(1 row)

SET
SET
SET
SET
CREATE EXTENSION
COMMENT
You are now connected to database "postgres" as user "postgres".
SET
SET
SET
SET
SET
SET
 set_config 
------------
(1 row)

SET
SET
SET
SET
COMMENT
CREATE EXTENSION
COMMENT
You are now connected to database "template1" as user "postgres".
SET
SET
SET
SET
SET
SET
 set_config 
------------
(1 row)

SET
SET
SET
SET
COMMENT
CREATE EXTENSION
COMMENT

5. まとめ

サービスの再起動とかをpostgresユーザーでやろうとしてハマったり、
ポート番号が違うのにpsqlで接続しようとしたりしてハマりましたが、
それ以外は問題ありませんでした。

古いパッケージは、そのうち削除すると思います。

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
What you can do with signing up
0