ねこものがたり

いちにちいっぽ

pgがバージョン違いで起動できなくなったときの対応

思い立ったものがあったのでrails newしました。 Herokuでさくっと公開したかったので -d postgresqlオプションを付けました。 するとDBが起動できなくてちょっと困りました。(DBが起動できないとrails sとかできない)

起きたエラー

$ pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2019-08-23 21:11:30.458 JST [17546] FATAL:  database files are incompatible with server
2019-08-23 21:11:30.458 JST [17546] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.4.
 stopped waiting
pg_ctl: could not start server
Examine the log output.

古いPostgreSQLのアップグレード

$ brew postgresql-upgrade-database
==> brew install postgresql@10
==> Downloading https://homebrew.bintray.com/bottles/postgresql@10-10.9.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/c8/c8ed8b8f9eb72d0da20bd4e45a9caa14b429a02366e5794b74e78be6ba236625?__gda__=exp=1566563171~hmac=27469c24ba0c6f093c1ce641bd93733c16
######################################################################## 100.0%
==> Pouring postgresql@10-10.9.mojave.bottle.tar.gz
==> /usr/local/Cellar/postgresql@10/10.9/bin/initdb /usr/local/var/postgresql@10
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

postgresql@10 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have postgresql@10 first in your PATH run:
  echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> ~/.bash_profile

For compilers to find postgresql@10 you may need to set:
  export LDFLAGS="-L/usr/local/opt/postgresql@10/lib"
  export CPPFLAGS="-I/usr/local/opt/postgresql@10/include"

For pkg-config to find postgresql@10 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/postgresql@10/lib/pkgconfig"


To have launchd start postgresql@10 now and restart at login:
  brew services start postgresql@10
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgresql@10 start
==> Summary
🍺  /usr/local/Cellar/postgresql@10/10.9: 1,708 files, 20.8MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/jpeg/9b... (20 files, 724KB)
Removing: /Users/keiko/Library/Caches/Homebrew/sqlite--3.28.0.mojave.bottle.tar.gz... (1.8MB)
Removing: /Users/keiko/Library/Logs/Homebrew/libtiff... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/postgresql... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/augeas... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/libpng... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/gdbm... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/icu4c... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/readline... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/sqlite... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/jpeg... (64B)
Removing: /Users/keiko/Library/Logs/Homebrew/mrbgem-template... (3 files, 368.4KB)
==> Upgrading postgresql data from 10 to 11...
waiting for server to start....2019-08-23 21:15:38.303 JST [18179] LOG:  listening on IPv6 address "::1", port 5432
2019-08-23 21:15:38.303 JST [18179] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-08-23 21:15:38.305 JST [18179] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-08-23 21:15:38.380 JST [18180] LOG:  database system was shut down at 2019-06-22 12:11:32 JST
2019-08-23 21:15:38.403 JST [18179] LOG:  database system is ready to accept connections
 done
server started
waiting for server to shut down....2019-08-23 21:15:38.609 JST [18179] LOG:  received fast shutdown request
2019-08-23 21:15:38.610 JST [18179] LOG:  aborting any active transactions
2019-08-23 21:15:38.611 JST [18179] LOG:  worker process: logical replication launcher (PID 18186) exited with exit code 1
2019-08-23 21:15:38.611 JST [18181] LOG:  shutting down
2019-08-23 21:15:38.623 JST [18179] LOG:  database system is shut down
 done
server stopped
==> Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old...
The files belonging to this database system will be owned by user "keiko".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Tokyo
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /usr/local/opt/postgresql/bin/pg_ctl -D /usr/local/var/postgres -l logfile start

Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    ./analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
==> Upgraded postgresql data from 10 to 11!
==> Your postgresql 10 data remains at /usr/local/var/postgres.old

結果

$ pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2019-08-23 21:19:03.720 JST [18432] LOG:  listening on IPv6 address "::1", port 5432
2019-08-23 21:19:03.720 JST [18432] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-08-23 21:19:03.723 JST [18432] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-08-23 21:19:03.754 JST [18433] LOG:  database system was shut down at 2019-08-23 21:15:47 JST
2019-08-23 21:19:03.762 JST [18432] LOG:  database system is ready to accept connections
 done
server started
$ rails db:create
Created database 'mytype_development'
Created database 'mytype_test'
$ rails s
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

おまけ

いえーい🙌 Image from Gyazo