As described by @Magne, the error `PG::ConnectionBad - could not connect to server: Connection refused` can be presented following a [major/minor version](
[To see links please register here]
) upgrade (e.g. `9.5 -> 9.6` or `9 -> 10`) of PostgreSQL.
I got this error after having run `brew upgrade postgresql` after the release of PostgreSQL version 9.6. The problem is that major/minor version upgrades require additional steps to migrate old date to the new version.
**How to check if this is your problem**
You can check if this is the problem by checking the latest brew formula PostgreSQL version installed with homebrew...
$ brew info postgresql
/usr/local/Cellar/postgresql/9.5.4_1 (3,147 files, 35M)
Poured from bottle on 2016-10-14 at 13:33:28
/usr/local/Cellar/postgresql/9.6.1 (3,242 files, 36.4M) *
Poured from bottle on 2017-02-06 at 12:41:00
...and then comparing it to the current PG_VERSION
$ cat /usr/local/var/postgres/PG_VERSION
9.5
If the PG_VERSION is less than the latest brew formula and the difference is a major/minor version change, then this is probably your problem.
**How to fix (i.e. how to upgrade the data)**
*Instructions below are for an upgrade from 9.5 to 9.6. Change the version numbers as appropriate for your own upgrade*
***Step 1.*** Make sure PostgreSQL is switched off:
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, with Homebrew...
$ brew services stop postgresql
***Step 2.*** Make a new pristine database:
$ initdb /usr/local/var/postgres9.6 -E utf8
***Step 3.*** Check what the old and new binary versions are:
$ ls /usr/local/Cellar/postgresql/
9.5.3 9.5.4 9.6.1
Note that in this example I am upgrading from 9.5.4 binary to 9.6.1 binary
***Step 4.*** Migrate the current data to the new database using the [pg_upgrade](
[To see links please register here]
) utility.
$ pg_upgrade \
-d /usr/local/var/postgres \
-D /usr/local/var/postgres9.6 \
-b /usr/local/Cellar/postgresql/9.5.4/bin/ \
-B /usr/local/Cellar/postgresql/9.6.1/bin/ \
-v
- `-d` flag specifies the current data directory
- `-D` flag specifies the new data directory to be created
- `-b` specifies the old binary
- `-B` specifies the new binary we're upgrading to
***Step 5.*** Move the old data directory out of the way
$ mv /usr/local/var/postgres /usr/local/var/postgres9.5
***Step 6.*** Move newly created data directory to where PostgreSQL expects it to be
$ mv /usr/local/var/postgres9.6 /usr/local/var/postgres
***Step 7.*** Start PostgreSQL again
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
# or, if you're running a current version of Homebrew
$ brew services start postgresql
***Step 8.*** If you’re using the pg gem for Rails, you should recompile by uninstalling and reinstalling the gem (skip this step if you're not using the pg gem)
$ gem uninstall pg
$ gem install pg
***Step 9.(optional)*** After you've reassured yourself that everything is working OK, you can run regain some disk space with the following command:
brew cleanup postgresql
...and if you're feeling really brave you can delete the old PostgreSQL data directory with the following command
rm -rf /usr/local/var/postgres9.5/
*(This answer is based on an excellent blog post
[To see links please register here]
with some additions)*