“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey
Senior Software Engineer, UK

Postgres isn't starting again

It’s just silently falling over and restarting when trying to start with brew services start postgresql

New-MacBook-Pro:local cex$ sudo -s tail -f /var/log/system.log
Password:
Feb 19 09:52:41 New-MacBook-Pro com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql[27587]): Service exited with abnormal code: 1
Feb 19 09:52:41 New-MacBook-Pro com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Feb 19 09:52:51 New-MacBook-Pro com.apple.xpc.launchd[1] (homebrew.mxcl.postgresql[27616]): Service exited with abnormal code: 1

Starting manually and checking the server.log

New-MacBook-Pro:local cex$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
New-MacBook-Pro:local cex$ cat /usr/local/var/
cache/    db/       homebrew/ lib/      log/      mongodb/  mysql/    postgres/ run/      
New-MacBook-Pro:local cex$ cat /usr/local/var/postgres/server.log 
2019-02-19 10:00:29.473 GMT [30912] FATAL:  database files are incompatible with server
2019-02-19 10:00:29.473 GMT [30912] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.1.

Ok, so data dir is incompatible. Damn. Hopefully I can just upgrade the data files with brew postgresql-upgrade-database

New-MacBook-Pro:local cex$ brew postgresql-upgrade-database
==> brew install postgresql@10
==> Downloading https://homebrew.bintray.com/bottles/postgresql@10-10.6_1.mojave.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring postgresql@10-10.6_1.mojave.bottle.1.tar.gz
==> /usr/local/Cellar/postgresql@10/10.6_1/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.6_1: 1,706 files, 20.8MB
==> Upgrading postgresql data from 10 to 11...
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
waiting for server to start....2019-02-19 10:05:34.199 GMT [32604] LOG:  listening on IPv6 address "::1", port 5432
2019-02-19 10:05:34.199 GMT [32604] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-02-19 10:05:34.200 GMT [32604] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-02-19 10:05:34.219 GMT [32605] LOG:  database system was shut down at 2019-02-07 21:30:24 GMT
2019-02-19 10:05:34.223 GMT [32604] LOG:  database system is ready to accept connections
 done
server started
waiting for server to shut down....2019-02-19 10:05:34.362 GMT [32604] LOG:  received fast shutdown request
2019-02-19 10:05:34.362 GMT [32604] LOG:  aborting any active transactions
2019-02-19 10:05:34.363 GMT [32604] LOG:  worker process: logical replication launcher (PID 32611) exited with exit code 1
2019-02-19 10:05:34.363 GMT [32606] LOG:  shutting down
2019-02-19 10:05:34.369 GMT [32604] 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 "cex".
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 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
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Fortunately that seems to have worked.

GPK of the Day Mad DONNA