Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 but was unable to find it...probably my fault. Anyone know how?

share|improve this question
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? – dezso Sep 12 '12 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 Sep 12 '12 at 5:22
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 – dezso Sep 12 '12 at 6:29
up vote 3 down vote accepted

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.

share|improve this answer
That'll do, thanks! – Meltemi Sep 12 '12 at 14:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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