In this walkthrough tutorial, I will install PostgreSQL on a Mac. Sometimes I may sound old school, but I insist on calling “things” by their name so PostgreSQL is PostgreSQL, not Postgre, not Postgres, not pgSQL… Feel free to add any other stupid acronyms / nickname in the comments!

Environment

  • Mac OS X 10.9.1 (13B42) – Mavericks
  • PostgreSQL 9.3.2-2

Downloads

PostgreSQL for Mac can be downloaded from http://www.postgresql.org/download/macosx/.

Starting the installation process

When you double click on the postgresql-9.3.2-2-osx.app icon (after you unzipped it), the system may warn you that he does not trust this application. You will need to authorize it. Open the System Preferences. Double click on Security & Privacy.

PostgreSQL Click on Security
PostgreSQL Click on Security

On the General tab, you will see a warning telling you which last application has not been allowed to run. Click on Open Anyway. Don’t do that if it is not PostgreSQL here (or any other unknown app).

PostgreSQL click on Open Aniway
PostgreSQL click on Open Anyway

Enter your credentials.

System password to install PostgreSQL
System password to install PostgreSQL

Follow the wizard.

PostgreSQL Setup
PostgreSQL Setup
Installation directory
Installation directory
Select PostrgreSQL data directory
Select PostrgreSQL data directory
Enter user and password
Enter user and password

Don’t try to be a smart ass, enter a password.

The installer will create (if you do not have it) a PostgreSQL user on your Mac. The user’s account name will be postgres. This is not the same user as the database user: if you forget the database server’s password, you cannot change it via the Users & Groups panel of System Preferences. To do so, psql contains a command \password that can be used to change a role’s password. It will do so without exposing the clear text password.

You really need a password
You really need a password
Default connection port to the PostgreSQL server
Default connection port to the PostgreSQL server
Select the Default Locale
Select the Default Locale
Everything is set
Everything is set
Files are being copied
Files are being copied

Make sure you allow connections to your server.

Mac OS X firewall warning, make sure you allow connection to your database
Mac OS X firewall warning, make sure you allow connection to your database
Installation completed, ready to install Stack Builder
Installation completed, ready to install Stack Builder

Installing other components

Simply follow the wizard.

Installation of Application Stack Builder
Installation of Application Stack Builder
Choose the right instance to work on
Choose the right instance to work on

Selection

Select what you need. In my case, I picked:

  • pgJDBC, JDBC driver v9.2-1001-1.
  • The Apache v2.4.7 and PHP v5.4.23-2 bundle.
  • phpPgAdmin v5.1-1 to administer the database.

If you plan on using Maven, it is not required to install it through Stack Builder.

Software selection: JDBC driver v9.2-1001-1, JDBC3 and JDBC4 drivers
Software selection: JDBC driver v9.2-1001-1, JDBC3 and JDBC4 drivers
Software selection: Apache v2.4.7 and PHP v5.4.23-2
Software selection: Apache v2.4.7 and PHP v5.4.23-2
Software selection: phpPgAdmin v5.1-1
Software selection: phpPgAdmin v5.1-1
Stack installation location
Stack installation location
Downloading remaining files
Downloading remaining files
All installation files are now downloaded, each package will be installed separately
All installation files are now downloaded, each package will be installed separately

Apache & PHP bundle

This is required if you want to have ready-to-use Apache server with PHP and PostgreSQL . It is also required if you plan to use phpPgAdmin, the web tool for managing the server.

Installation of Apache and PHP
Installation of Apache and PHP
Setting up the embeded Apache PHP bundle
Setting up the embedded Apache PHP bundle
Installation directory
Installation directory
Specifying Apache port number
Specifying Apache port number
Ready to install
Ready to install
Installation finished
Installation finished

JDBC driver

PostgreSQL comes with a JDBC type 3 and type 4 driver. It is called pgJDBC. It is not required if you plan to use Maven.

Installation of the JDBC driver
Installation of the JDBC driver
Installation directory
Installation directory
PostgreSQL JDBC Installer ready
PostgreSQL JDBC Installer ready
PostgreSQL JDBC installation finished
PostgreSQL JDBC installation finished

phpPgAdmin

Installing phpPgAdmin
Installing phpPgAdmin
Installation details
Installation details

The host should read 127.0.0.1 not 127.0.0.15.

Installer is ready
Installer is ready
Installing...
Installing…
phpPgAdmin installation is finished
phpPgAdmin installation is finished

Conclusion

Stack Builder installation completed
Stack Builder installation completed

Your server is now up and running. We can now test that everything is fine.

Testing

There is 3 tools you can try: UI, command line and the web…

Web

If you installed the Apache module connect to http://localhost:8080/phpPgAdmin/.

Connection to phpPgAdmin
Connection to phpPgAdmin

Once you’re connected, you will see all the information:

Details of the database information through phpPgAdmin
Details of the database information through phpPgAdmin

 Command line

You can run psql from the terminal if you have set up all the environment variables correctly. You can also run it from the Applications (and PostgreSQL 9.3 folder).

SQL Shell (psql) from the Applications folder
SQL Shell (psql) from the Applications folder

UI tool: pgAdmin III

I have seen nicer admin tools, but pgAdmin III will do the job if you like the point and click interface. I liked the fact that the tool can generate SQL commands as you “point and click”, which allows you to run them in psql with modification if you need to.

pgAdmin III
pgAdmin III

If you have trouble, check the troubleshooting.

Troubleshooting

 Checking that PostgreSQL runs

Open a terminal and run:

$ ps auxwww | grep postgres

You should get something like:

postgres          234   0,0  1,3  2718932 216712   ??  Ss   Dim04     0:15.86 com.apple.IconServicesAgent
postgres          233   0,0  0,0  2496600   2696   ??  Ss   Dim04     0:00.15 /usr/libexec/xpcd
postgres          171   0,0  0,0  2510160    744   ??  Ss   Dim04     0:03.94 postgres: stats collector process    
postgres          170   0,0  0,0  2654608   2484   ??  Ss   Dim04     0:01.62 postgres: autovacuum launcher process    
postgres          169   0,0  0,0  2638092    756   ??  Ss   Dim04     0:01.82 postgres: wal writer process    
postgres          168   0,0  0,0  2638092   1592   ??  Ss   Dim04     0:01.94 postgres: writer process    
postgres          167   0,0  0,0  2638092    836   ??  Ss   Dim04     0:00.06 postgres: checkpointer process    
postgres          162   0,0  0,0  2485584    520   ??  Ss   Dim04     0:00.00 postgres: logger process    
postgres          101   0,0  0,1  2639116  13832   ??  Ss   Dim04     0:01.21 /Library/PostgreSQL/9.3/bin/postmaster -D/Library/PostgreSQL/9.3/data
jgp             18603   0,0  0,0  2452240    684 s003  S+    1:33     0:00.00 grep postgres
postgres        18595   0,0  0,0  2518912   5992   ??  SN    1:32     0:00.04 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared
postgres        18594   0,0  0,0  2518912   5952   ??  SN    1:32     0:00.04 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared
postgres        18593   0,0  0,0  2494336   5952   ??  SN    1:32     0:00.04 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared
postgres        18559   0,0  0,0  2490000   5812   ??  S     1:28     0:00.08 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker-sizing -c MDSSizingWorker -m com.apple.mdworker.sizing
postgres        18558   0,0  0,0  2514280   1252   ??  S     1:28     0:00.02 /usr/sbin/cfprefsd agent
postgres        18557   0,0  0,0  2529128   1664   ??  U     1:28     0:00.02 /usr/sbin/distnoted agent
postgres        18552   0,0  0,0  2508252   1004   ??  Ss    1:28     0:00.03 /sbin/launchd