Skip to content
SQLEye

Blog / Local Development

Run multiple PostgreSQL versions side by side

By CodeROG · Published · Last updated

You can run several PostgreSQL major versions on one machine by giving each server its own data directory, created with initdb -D, and its own TCP port, passed with pg_ctl start -o "-p PORT". Each version also needs its own set of binaries. Nothing else is shared, so the servers do not interfere.

Why run more than one version?

  • Testing an upgrade. Restore a copy of your data into the next major version and run your application against it before touching production.
  • Parity with production. If production runs 16, develop against 16, not whatever version your package manager installed.
  • Working on several projects that target different versions at the same time.
  • Comparing behaviour. A query plan or a setting default can differ between major versions; two servers let you run the same query on both.

How do I do it by hand?

Assume you have the binaries for each version in separate directories. How you get them (a package manager, a prebuilt archive, a source build) depends on your OS, so the paths below are placeholders. The commands are the same everywhere.

  1. Create one data directory per server with initdb. -D sets where the cluster is stored, and -U sets the bootstrap superuser name (it defaults to the operating system user running initdb):

    Terminal window
    /opt/pg16/bin/initdb -D "$HOME/pgdata/16" -U postgres
    /opt/pg17/bin/initdb -D "$HOME/pgdata/17" -U postgres

    initdb must run as the user that will own the server process, and it refuses to run as root. The manual warns that trust is the default authentication for ease of installation and says not to use it unless you trust all local users; read the --auth options before you use a machine shared with others.

  2. Start each server on a different port. -D selects the data directory, -l appends server log output to a file, and -o passes options straight to the postgres command, which is where the port goes:

    Terminal window
    /opt/pg16/bin/pg_ctl -D "$HOME/pgdata/16" -o "-p 5432" -l "$HOME/pgdata/16.log" start
    /opt/pg17/bin/pg_ctl -D "$HOME/pgdata/17" -o "-p 5433" -l "$HOME/pgdata/17.log" start

    Note that pg_ctl -p is something else: it gives the path to the postgres executable, not a port.

  3. Connect to each one by port and check what you are talking to:

    Terminal window
    psql -h 127.0.0.1 -p 5432 -U postgres -d postgres -c "SHOW server_version;"
    psql -h 127.0.0.1 -p 5433 -U postgres -d postgres -c "SHOW server_version;"
  4. Check or stop a server with the same -D:

    Terminal window
    /opt/pg17/bin/pg_ctl -D "$HOME/pgdata/17" status
    /opt/pg17/bin/pg_ctl -D "$HOME/pgdata/17" stop -m fast

    status prints the server’s process ID and start options, and exits with code 3 if the server is not running. fast mode (the default) rolls back active transactions and disconnects clients before shutting down.

To make the port permanent, set port = 5433 in that cluster’s postgresql.conf. It can only be set at server start, so changing it needs a restart.

What are the pitfalls?

Pitfall What happens What to do
Two servers on one port The second server fails to start Give every server a unique port
Data directory from another major version The server refuses to use it Create a new cluster, then migrate the data
Wrong psql for the server Backslash commands can fail Use the newest psql, or the one that matches the server
Mixing up which version’s tools you run You run pg_ctl or initdb from the wrong install Call each binary by full path, as above
Forgetting the port You change data on the wrong server Always pass -p, and check with SHOW server_version

Is a data directory tied to a major version?

Yes. For major releases the manual says the internal data storage format is subject to change, and that checks are in place that prevent you from using a data directory with an incompatible version of PostgreSQL. To move data to a new major version, use one of the two methods in the manual:

  • A logical dump and restore. The manual shows a way to run both servers in parallel and pipe one into the other:

    Terminal window
    pg_dumpall -p 5432 | psql -d postgres -p 5433
  • pg_upgrade, which migrates a cluster in place without a dump and restore. Its required options are -b (old bin directory), -d (old data directory) and -D (new data directory); -B (new bin directory) defaults to the directory pg_upgrade is in. --check checks the clusters without changing any data, and --link uses hard links instead of copying files, which is faster but means you cannot use the old cluster once the new one has started. Per the manual, when checking an old server that is still running, the old and new port numbers must be different.

Run a --check against a copy of your data first. This is where a second local version pays off.

Which psql should I use?

The manual says psql works best with servers of the same or an older major version, and backslash commands are particularly likely to fail if the server is newer than psql. It recommends using the newest psql when you connect to several major versions, or keeping a copy of psql for each version.

How does SQLEye do it?

SQLEye is not released yet and there are no downloads. This describes designed behaviour.

SQLEye is a free desktop app that runs local PostgreSQL and MySQL instances. The steps above are what it automates. Each instance is one server with its own port and its own data directory, so running several versions at once is the normal case rather than a special setup. As of 2026-11-29 its catalogue has PostgreSQL 15.20, 16.16 and 17.12, downloaded on demand and checksum-verified; these numbers move as patch releases ship.

  • The default port for PostgreSQL is 5432. If it is taken, SQLEye suggests the next free port above it. If you pick a port that is busy, starting fails with a message telling you to change the port.
  • Every instance’s files live under the app data directory, in instances/<id>/data.
  • Servers bind to 127.0.0.1 only.
  • You can change an instance’s port only while it is stopped, and several instances can run at the same time.

See running versions side by side for the workflow, ports for how ports are picked, and data location for where files go. The PostgreSQL engine page lists the supported versions.

Documentation: Run several versions side by side. Run PostgreSQL 15, 16, 17 and MySQL 8.0 or 8.4 at the same time in SQLEye, each instance with its own port and data directory.

postgresqlversionslocal development

Related posts

Written by CodeROG, who develop SQLEye. CodeROG develops and maintains SQLEye, a free desktop app for running local PostgreSQL and MySQL and watching every query live. This post is also available as Markdown.