An easy way to install and manage PostgreSQL on Mac

Photo by Waldemar on Unsplash

An easy way to install and manage PostgreSQL on Mac

Postgres.app is a full-featured PostgreSQL installation packaged as a standard Mac app. It includes everything you need to get started.

Installation

Download your version of PostgreSQL from here

The dmg file comes with PostgreSQL and postgresapp GUI to manage.

Installation Directories

  • Binaries: /Applications/Postgres.app/Contents/Versions/latest/bin

  • Headers: /Applications/Postgres.app/Contents/Versions/latest/include

  • Libraries: /Applications/Postgres.app/Contents/Versions/latest/lib

  • Default data directory: ~/Library/Application Support/Postgres/var-XX (XX is the major version of PostgreSQL)

You can also view these directories using the postgresapp GUI

Now you can connect with the server using psql or your favourite GUI tools:

There are many clients for PostgreSQL on the Mac. Below is a list of some of the Mac Apps I found (in alphabetic order).

If you need to provide an URL, use postgresql://YOURUSERNAME@localhost/YOURUSERNAME

OR with a password,

postgresql://YOURUSERNAME:YOURPASSWORD@localhost/YOURUSERNAME

Uninstalling

  1. Quit Postgres.app & drag it to the Trash

  2. Delete the data directory (default location: ~/Library/Application Support/Postgres)

  3. Delete preferences for Postgres.app by executing the following command:
    defaults delete com.postgresapp.Postgres2

  4. Remove the $PATH config for the command line tools (optional):
    sudo rm /etc/paths.d/postgresapp

Upgrading Major Version

When upgrading to a new major version of PostgreSQL, you need to migrate your data. The easiest way to migrate your data is using pg_dumpall, but there are alternative methods that can be useful when you have very large databases, or if you want to migrate only parts of the database.

Migrate data using pg_dumpall

This is the easiest way to migrate your data.

  1. Make sure that the old server is running

  2. Create a compressed SQL dump of your server (this could take some time):
    pg_dumpall --quote-all-identifiers | gzip >postgresapp.sql.gz

  3. Stop the old server, then start the new server.

  4. Download and install the desired major version from here, during installation, choose replace

  5. Now restore the SQL dump:
    gunzip <postgresapp.sql.gz | psql

  6. Don’t forget in Server Settings.. to swap which server to start automatically

Did you find this article valuable?

Support Sujeet Agrahari by becoming a sponsor. Any amount is appreciated!