How can I run two copies of PostgreSQL on Mac OS X?

3.3k views Asked by At

I need to run integration tests for a system that has both a local and a remote database server; I'd like on my dev Mac to have two PostgreSQL servers on different ports. I usually use Postgres.app - can this be somehow configured to run two instances?

4

There are 4 answers

4
d1ll1nger On BEST ANSWER

Just call initdb from the binary directory (/Applications/Postgres.app/Contents/Versions/latest/bin/initdb). Pass --help if you wish but initdb only needs a data directory so:

initdb -D <dir>

Once complete go to your new directory and edit postgresql.conf and change the port.

Then start up

0
skplunkerin On

The PostgresApp does have this ability, although it's hidden. (this answer is 3 years late, the app might not have had this back then)

Download Link

When you open the App, open the sidebar (source) and click the + icon to add another server. You can choose the name / version / port for each server.

Make sure to setup the CLI tools (source) if you haven't already.

sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

If you want to have different psql commands for each server, add some useful aliases to your ~/.profile:

# Name them whatever you want
alias "psql-project1"="psql -h localhost -p 5432"
alias "psql-project2"="psql -h localhost -p 5433"

As of 2020/03/30, the download page lets you install different versions of the PostgresApp that supports the following versions:

  • Postgres.app with PostgreSQL 12.1
  • Postgres.app with PostgreSQL 10.11, 11.6, and 12.1
  • Postgres.app with all currently supported PostgreSQL versions (12.1, 11.6, 10.11, 9.6.16, 9.5.20, 9.4.25)
0
Basil Bourque On

Virtual Machines

Another option is running each Postgres cluster (installation) inside a virtual machine on your Mac.

Parallels

I have done this with Parallels Desktop For Mac versions 9, 10, and 11. (Version 12 is current, but I've not tried it with Postgres yet.)

I especially like using a VM for Postgres as this serious database tends to be a rather heavyweight installation when not using Postgres.app, such as when using the installers provided by EnterpriseDB.com as a courtesy to the community.

Networking

You can configure the networking in Parallels to either be completely isolated, share the host Mac’s network identity, or assign the VM its own IP address and act as a separate computer on the network. Choose Configure… > Hardware > Network 1. In the Source pop-up menu you can choose one of the following network types for the virtual machine network adapter (description excerpted from online Help):

  • Disconnect
  • Shared Network
    Select this option to enable the Network Address Translation (NAT) feature for the virtual machine. In this case, your virtual machine will share whatever network connection is currently used by the host computer.
  • Bridged Network
    Select this option to allow the virtual machine to access the local network and Internet through one of the network adapters installed on the host computer. In this case, the virtual machine is treated as a stand-alone computer on the network and should be configured in the same way as a real one. You can choose the physical adapter where the virtual machine adapter will be bridged in the list below the Bridged Network option.
  • Host-Only Network
    Select this option to allow the virtual machine to connect to the host computer and the virtual machines residing on it and to make it invisible outside the host computer.

You could configure each VM with its own IP number while the Postgres inside each uses the same default port of 5432. Tip: Use ping command-line tool to verify that the guest and host macOS can reach each other.

Testing

Virtual machines can help with testing.

You can test macOS this way, running older or newer versions of macOS as a guest. The guest macOS version need not be the same as the host.

Likewise, you could have different versions of Postgres installed in different VMs for testing purposes.

You can make a back-up copy of the single file that represents your entire VM (shut down the VM first of course). Any time you want to restore to its original condition, replace the current VM file with a fresh copy of the backup.

Other virtual machines

Parallels is but one of three successful virtual machine apps available for macOS (Mac OS X). I expect all three would work well hosting a Postgres server.

Apple does now allow installing recent versions of macOS as a guest OS in a virtual machine when running on a Mac as the host. (Formerly not allowed many years ago.) Read their license for details.

0
Michael Dillon On

You may find this easier to manage if you install two different distros of Postgresql. Choose two of the ones at https://www.postgresql.org/download/macosx/

If you have never installed Fink, or Homebrew or Macports, I recommend choosing Macports because it interferes less with other tools.

The port number to use is configured in postgresql.conf. Just change the line that says

port = 5432

and restart that PostgreSQL server.