In order to install PostgreSQL on OSX, you need to know which versions are currently supported.
Use this command to see what versions you have available.
sudo port list | grep "^postgresql[[:digit:]]\{2\}[[:space:]]"
You should get a list that looks something like the following:
postgresql80 @8.0.26 databases/postgresql80
postgresql81 @8.1.23 databases/postgresql81
postgresql82 @8.2.23 databases/postgresql82
postgresql83 @8.3.23 databases/postgresql83
postgresql84 @8.4.22 databases/postgresql84
postgresql90 @9.0.23 databases/postgresql90
postgresql91 @9.1.22 databases/postgresql91
postgresql92 @9.2.17 databases/postgresql92
postgresql93 @9.3.13 databases/postgresql93
postgresql94 @9.4.8 databases/postgresql94
postgresql95 @9.5.3 databases/postgresql95
postgresql96 @9.6beta2 databases/postgresql96
In this example, the most recent version of PostgreSQL that is supported in 9.6, so we will install that.
sudo port install postgresql96-server postgresql96
You will see an installation log like this:
---> Computing dependencies for postgresql96-server
---> Dependencies to be installed: postgresql96
---> Fetching archive for postgresql96
---> Attempting to fetch postgresql96-9.6beta2_0.darwin_15.x86_64.tbz2 from https://packages.macports.org/postgresql96
---> Attempting to fetch postgresql96-9.6beta2_0.darwin_15.x86_64.tbz2.rmd160 from https://packages.macports.org/postgresql96
---> Installing postgresql96 @9.6beta2_0
---> Activating postgresql96 @9.6beta2_0
To use the postgresql server, install the postgresql96-server port
---> Cleaning postgresql96
---> Fetching archive for postgresql96-server
---> Attempting to fetch postgresql96-server-9.6beta2_0.darwin_15.x86_64.tbz2 from https://packages.macports.org/postgresql96-server
---> Attempting to fetch postgresql96-server-9.6beta2_0.darwin_15.x86_64.tbz2.rmd160 from https://packages.macports.org/postgresql96-server
---> Installing postgresql96-server @9.6beta2_0
---> Activating postgresql96-server @9.6beta2_0
To create a database instance, after install do
sudo mkdir -p /opt/local/var/db/postgresql96/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql96/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb'
---> Cleaning postgresql96-server
---> Computing dependencies for postgresql96
---> Cleaning postgresql96
---> Updating database of binaries
---> Scanning binaries for linking errors
---> No broken files found.
The log provides instructions on the rest of the steps for installation, so we do that next.
sudo mkdir -p /opt/local/var/db/postgresql96/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql96/defaultdb
sudo su postgres -c '/opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb'
Now we start the server:
sudo port load -w postgresql96-server
Verify that we can connect to the server:
su postgres -c psql
You will see a prompt from postgres:
psql (9.6.1)
Type "help" for help.
postgres=#
Here you can type a query to see that the server is running.
postgres=#SELECT setting FROM pg_settings WHERE name='data_directory';
And see the response:
setting
------------------------------------------
/opt/local/var/db/postgresql96/defaultdb
(1 row)
postgres=#
Type \q to quit:
postgres=#\q
And you will be back at your shell prompt.
Congratulations! You now have a running PostgreSQL instance on OS/X.