Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Notes on Setting Up PostgreSQL

Matthew Zagaja edited this page Feb 28, 2020 · 4 revisions

Notes on Setting Up Postgres

Many Mac users have struggled setting up PostgreSQL with Homebrew. Since I often find myself repeating the same troubleshooting steps I'm providing them here. This assumes you have already installed Homebrew and RVM. If you are looking for an initial setup of your development environment go to http://installrails.com.

Step 1

brew install postgres

You will receive the following message which has some good advice, but might not be the best way to proceed:

If builds of PostgreSQL 9 are failing and you have version 8.x installed, you may need to remove the previous version first. See: https://github.com/Homebrew/homebrew/issues/2510

To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see: http://www.postgresql.org/docs/9.5/static/upgrading.html

To migrate existing data from a previous minor version (9.0-9.4) of PosgresSQL, see: http://www.postgresql.org/docs/9.5/static/pgupgrade.html

You will need your previous PostgreSQL installation from brew to perform pg_upgrade. Do not run brew cleanup postgresql until you have performed the migration.

To have launchd start postgresql at login: ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents Then to load postgresql now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist Or, if you don't want/need launchctl, you can just run: postgres -D /usr/local/var/postgres

Step 2

Instead of following the instructions above I recommend following Thoughtbot's Instructions for installing brew services. Then you should be able to run brew services start postgresql to start Postgres.

Step 3

Test connecting to your database by typing psql in a console. Sometimes it fails due to an issue referenced here.

If you are lucky you might just need to do:

brew postgresql-upgrade-database

Otherwise the quick and easy fix is:

rm -rf /usr/local/var/postgres && \
initdb /usr/local/var/postgres -E utf8 && \
brew services restart postgresql && \
createdb $(whoami)

You may want to read this comment for more detail or if you had a previous Postgres version installed.