PostgreSQL on macOS
Installing PostgreSQL on macOS
Install MacPorts: See InstallingMacPorts, then:
$ sudo port install postgresql18-server
For versions of PostgreSQL before 16, optionally, install the documentation (newer versions install the documentation by default):
$ sudo port install postgresql15-doc
See the notes that are listed after the installation and follow the instructions therein.
Thereafter the server can be started and stopped with:
$ sudo port load postgresql18-server
$ sudo port unload postgresql18-server
Upgrading
These instructions assume you are upgrading from PostgreSQL 17 to PostgreSQL 18.
Install the documentation as described above and see
/opt/local/share/doc/postgresql18/html/upgrading.html$ open /opt/local/share/doc/postgresql18/html/upgrading.htmlInstall the new version of PostgreSQL using MacPorts as shown in the notes after installing the server port.
$ sudo port select postgresql postgresql18 $ sudo mkdir -p /opt/local/var/db/postgresql18/defaultdb $ sudo chown postgres:postgres /opt/local/var/db/postgresql18/defaultdbCreate the database instance:
$ sudo -u postgres /bin/sh -c 'cd /opt/local/var/db/postgresql18 && \ /opt/local/lib/postgresql18/bin/initdb -D \ /opt/local/var/db/postgresql18/defaultdb' $ sudo port load postgresql18-serverLeave the old database version running and create a dump of the existing database, using the new version of
pg_dumpall$ port select --list postgresql $ port select --show postgresqlCreate a database backup using the newer
pg_dumpall:$ /opt/local/lib/postgresql18/bin/pg_dumpall >backup.dmpStop the existing database server:
$ sudo port unload postgresql17-serverUpdate the new configuration file:
$ sudo diff -u /opt/local/var/db/postgresql17/defaultdb/pg_hba.conf \ /opt/local/var/db/postgresql18/defaultdb/pg_hba.conf $ sudoedit /opt/local/var/db/postgresql18/defaultdb/pg_hba.confUse the
port selectcommand to switch to the newly installed PostgreSQL binaries$ sudo port select --set postgresql postgresql18Optionally, if using PostGIS, upgrade that
postgistoo:$ port search -n 'pg*-postgis*' $ sudo port install pg18-postgisStart the new version of the server:
$ sudo port load postgresql18-serverAs the Unix PostgreSQL user, create the superuser role in the new database:
$ cd / $ sudo -u postgres createuser -drs $USER_NAME $ cd - $ psql -lRestore the database from the dump created earlier:
$ psql -d postgres -f backup.dmp
Trouble-shooting
Check the PostgreSQL log:
$ tail -n 20 /opt/local/var/log/postgresql18/postgres.log
-- Frank Dean - 14 May 2025
-- Frank Dean - 26 Jun 2018
Related Topics: PostgreSQL, InstallingMacPorts, MacOSXTips