How do I install the Postgres command-line client (psql) on OS X using MacPorts?

Solution 1:

Macports packages several versions of PosgreSQL where each version's packages' names contain the version string. So if XY is the concatenation of major and minor PostgreSQL release version (from 8.1 through 9.4 beta as of this writing), the packages are named like this:

  • postgresqlXY (This installs the client, 'psql'.)
  • postgresqlXY-server (This is the server portion.)
  • postgresqlXY-doc (And so on.)

You simply need to activate the installed version of your choice. If you install the package "postgresqlXY" (any version), you also get the package "postgresql_select" as a dependency, which lets you query the active version of postgresql:

$ port select --list postgresql
    Available versions for postgresql:
    none (active)
    postgresqlXY

You can specify the active version like this:

$ port select --set postgresql postgresqlXY
Selecting 'postgresqlXY' for 'postgresql' succeeded. 'postgresqlXY' is now active.

That causes Macports to place a symlink from /opt/local/bin/psql (which is on the path because you installed Macports) to the binary installed at /opt/local/lib/postgresqlXY/bin/psql (which isn't on the path):

$ which psql # That created this symlink to the active version:
/opt/local/bin/psql

Note that you can always access version X.Y of the client binary via /opt/local/bin/psqlXY, which is another symlink Macports maintains for each installed version no matter what is or isn't selected. It was there before you selected version XY, and likewise remains if you select "none" to remove the standard binary name from your path:

$ port select postgresql none
Selecting 'none' for 'postgresql' succeeded. 'none' is now active.
$ which psql   # Not on the path because 'none' is active!
$ which psqlXY # But this is always present:
/opt/local/bin/psqlXY

People end up needing ready access to multiple versions of various pieces of software, and Macports makes that easy by packaging multiple versions of about 50 popular examples using this selection mechanism and corresponding somesoftware_select package as dependency. View the list:

$ port search _select

Solution 2:

Found the answer at: http://www.istarelworkshop.com/2011/01/04/snow_leopard_development_server_postgresql

Like the general MacPorts installation, the path to PostgreSQL command-line tools is not known to the system, so I append /opt/local/lib/postgresql84/bin in /etc/paths.

It seems that MacPorts installs the executable but doesn't put it in the path.