I have 2 instances of PostgreSQL running on some servers. One on the default port (5432) and the other instance on port 5433. Some users (and processes) only need access to the second instance (5433) and I'd like to set it up so that when those users use commands like psql or createdb in their shell it will automatically direct them to the right Postgres instance instead of them having to type -p 5433 along with every command.

I tried to look this up on www.postgresql.org but was unable to find it...probably my fault. Anyone know how?

  • A few things have to be clarified here: what OS do users use, how does the network environment of PostgreSQL look like (well, is that on a different box from the one which the users log on). And how often so they createdb? Commented Sep 12, 2012 at 4:05
  • Mac OS X Server. in this case the users are logged into (ssh) the machine. The version of PostgreSQL that came with the server is somewhat out of date and devoted to system services. I've installed another instance for user processes and Rails apps. We only createdb when creating a new Rails application so not too often but ever couple weeks. Using psql happens more often, which prompted this inquiry. Thanks for your help!
    – Meltemi
    Commented Sep 12, 2012 at 5:22
  • 1
    An idea (I don't know whether you have the means to do this): for running patches against a test DB here, I use an alias like test_patch, which is really psql -h [some IP] -p 5432 -d testdb Commented Sep 12, 2012 at 6:29

1 Answer 1


PostgreSQL command-line utilities (and more generally all programs that rely on the libpq library) automatically use the environment variables PGPORT and PGHOST when they're defined.

So if you do in the shell:

$ PGPORT=5433; export PGPORT

any subsequent call to psql will act as if it has been invoked with the -p 5433 command-line option.

See Environment Variables in the libpq documentation for all these variables . They can be used to provide default values to almost every parameter of a connection to the database.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.