MacでPostgreSQLが使えなくなったのを解決した話
背景
Homebrewでインストールしたpostgresqlに、突如入れなくなったので、対応をまとめておいた。
結論から言うと、 brew upgrade
で気づいたらpostgresqlのメジャーバージョンが上がっていた、という罠。
経緯
入れない
$ psql psql: error: could not connect to server: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
サービスは起動してた
$ brew services start postgresql Service `postgresql` already started, use `brew services restart postgresql` to restart.
再インストールしてみたがだめだった
$ brew reinstall postgresql ==> Reinstalling postgresql ==> Downloading https://homebrew.bintray.com/bottles/postgresql-12.1.catalina.bottle.tar.gz Already downloaded: /Users/yu.kobayashi/Library/Caches/Homebrew/downloads/9c1e9459a75290edc60c44b88a078c693b6a5ebfb2f2af7ddbb13f7e76914399--postgresql-12.1.catalina.bottle.tar.gz ==> Pouring postgresql-12.1.catalina.bottle.tar.gz ==> Caveats To migrate existing data from a previous major version of PostgreSQL run: brew postgresql-upgrade-database To have launchd start postgresql now and restart at login: brew services start postgresql Or, if you don't want/need a background service you can just run: pg_ctl -D /usr/local/var/postgres start ==> Summary 🍺 /usr/local/Cellar/postgresql/12.1: 3,217 files, 37.7MB $ psql psql: error: could not connect to server: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
気づき
再インストールのとき、よく見ると、
==> Downloading https://homebrew.bintray.com/bottles/postgresql-12.1.catalina.bottle.tar.gz
お?? 12?To migrate existing data from a previous major version of PostgreSQL run: brew postgresql-upgrade-database
お??
解決策
postgresが11から12にバージョン上がって、DBやら設定やらをアップデートかけなきゃいけなかったらしい、という話。いわれたとおりにコマンドをたたく。
$ brew postgresql-upgrade-database ==> brew install postgresql@11 ==> Downloading https://homebrew.bintray.com/bottles/postgresql@11-11.6.catalina.bottle.tar.gz ==> Downloading from https://akamai.bintray.com/c9/c91e358723fd5f700f059a0a1beff478d3e4aaa2ec40a3a166e946ff130d9fd0?__gda__=exp=1574911628~hmac=574662 ######################################################################## 100.0% ==> Pouring postgresql@11-11.6.catalina.bottle.tar.gz ==> /usr/local/Cellar/postgresql@11/11.6/bin/initdb --locale=C -E UTF-8 /usr/local/var/postgresql@11 ==> Caveats To migrate existing data from a previous major version of PostgreSQL run: brew postgresql-upgrade-database postgresql@11 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@11 first in your PATH run: echo 'export PATH="/usr/local/opt/postgresql@11/bin:$PATH"' >> ~/.zshrc For compilers to find postgresql@11 you may need to set: export LDFLAGS="-L/usr/local/opt/postgresql@11/lib" export CPPFLAGS="-I/usr/local/opt/postgresql@11/include" For pkg-config to find postgresql@11 you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/postgresql@11/lib/pkgconfig" To have launchd start postgresql@11 now and restart at login: brew services start postgresql@11 Or, if you don't want/need a background service you can just run: pg_ctl -D /usr/local/var/postgresql@11 start ==> Summary 🍺 /usr/local/Cellar/postgresql@11/11.6: 3,191 files, 36.4MB ==> Upgrading postgresql data from 11 to 12... Stopping `postgresql`... (might take a while) ==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql) waiting for server to start....2019-11-28 12:15:33.884 JST [7885] LOG: listening on IPv6 address "::1", port 5432 2019-11-28 12:15:33.884 JST [7885] LOG: listening on IPv4 address "127.0.0.1", port 5432 2019-11-28 12:15:33.886 JST [7885] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2019-11-28 12:15:33.908 JST [7886] LOG: database system was interrupted; last known up at 2019-11-22 10:49:27 JST ...2019-11-28 12:15:37.323 JST [7886] LOG: database system was not properly shut down; automatic recovery in progress 2019-11-28 12:15:37.326 JST [7886] LOG: redo starts at 0/206DDC0 2019-11-28 12:15:37.326 JST [7886] LOG: invalid record length at 0/206DEA0: wanted 24, got 0 2019-11-28 12:15:37.326 JST [7886] LOG: redo done at 0/206DE68 2019-11-28 12:15:37.337 JST [7885] LOG: database system is ready to accept connections done server started waiting for server to shut down...2019-11-28 12:15:37.720 JST [7885] LOG: received fast shutdown request .2019-11-28 12:15:37.720 JST [7885] LOG: aborting any active transactions 2019-11-28 12:15:37.721 JST [7885] LOG: background worker "logical replication launcher" (PID 7892) exited with exit code 1 2019-11-28 12:15:37.721 JST [7887] LOG: shutting down 2019-11-28 12:15:37.728 JST [7885] LOG: database system is shut down done server stopped ==> Moving postgresql data from /usr/local/var/postgres to /usr/local/var/postgres.old... ==> Creating database... The files belonging to this database system will be owned by user "yu.kobayashi". This user must also own the server process. The database cluster will be initialized with locale "C". 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 dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Asia/Tokyo creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: 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 ==> Migrating and upgrading data... 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 Checking for tables WITH OIDS ok Checking for invalid "sql_identifier" user columns 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 11 to 12! ==> Your postgresql 11 data remains at /usr/local/var/postgres.old ==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
解決した
$ psql postgres psql (12.1) Type "help" for help. postgres=# \q