Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql port confusion 5433 or 5432?

I have installed postgresql on OSX. When I run psql, I get

$ psql
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5433"?

However, from /etc/services

postgresql      5432/udp    # PostgreSQL Database
postgresql      5432/tcp    # PostgreSQL Database
#                          Tom Lane <[email protected]>
pyrrho          5433/tcp    # Pyrrho DBMS
pyrrho          5433/udp    # Pyrrho DBMS

5433 is occupied by pyrrho, 5432 is assigned to pg. I can connect with

psql -p 5432

but why does psql think it is 5433 and how do I make psql look in the right place by default?

like image 844
highBandWidth Avatar asked Oct 26 '22 21:10

highBandWidth


People also ask

What port number should I use for PostgreSQL?

Connecting to Your Database The PostgreSQL database service is available on localhost and the default PostgreSQL port is 5432 .

Is Postgres running on port 5432?

Usually Postgres is the only app interested in using port 5432, but if after issuing the first command to see what is running on port 5432, you find out that there is an application other than PostgreSQL running on port 5432, try to check the activity monitor and see what app might be interfering with your PostgreSQL ...

What is the use of port number 5432?

5432 (PostgreSQL) is used for Adaptive Authentication (TCP). Port 5432 is opened for the Postgres database used in the Behavioral Analytics feature of PCS. While scanning, customers may raise queries on 5432 Port as this port is enabled on the internal interface. However, an attacker cannot connect to it.

What is the default port the PostgreSQL server listens on?

By default, the Postgresql database listens on port 5432.


2 Answers

/etc/services is only advisory, it's a listing of well-known ports. It doesn't mean that anything is actually running on that port or that the named service will run on that port.

In PostgreSQL's case it's typical to use port 5432 if it is available. If it isn't, most installers will choose the next free port, usually 5433.

You can see what is actually running using the netstat tool (available on OS X, Windows, and Linux, with command line syntax varying across all three).

This is further complicated on Mac OS X systems by the horrible mess of different PostgreSQL packages - Apple's ancient version of PostgreSQL built in to the OS, Postgres.app, Homebrew, Macports, the EnterpriseDB installer, etc etc.

What ends up happening is that the user installs Pg and starts a server from one packaging, but uses the psql and libpq client from a different packaging. Typically this occurs when they're running Postgres.app or homebrew Pg and connecting with the psql that shipped with the OS. Not only do these sometimes have different default ports, but the Pg that shipped with Mac OS X has a different default unix socket path, so even if the server is running on the same port it won't be listening to the same unix socket.

Most Mac users work around this by just using tcp/ip with psql -h localhost. You can also specify a port if required, eg psql -h localhost -p 5433. You might have multiple PostgreSQL instances running so make sure you're connecting to the right one by using select version() and SHOW data_directory;.

You can also specify a unix socket directory; check the unix_socket_directories setting of the PostgreSQL instance you wish to connect to and specify that with psql -h, e.g.psql -h /tmp.

A cleaner solution is to correct your system PATH so that the psql and libpq associated with the PostgreSQL you are actually running is what's found first on the PATH. The details of that depend on your Mac OS X version and which Pg packages you have installed. I don't use Mac and can't offer much more detail on that side without spending more time than is currently available.

like image 222
Craig Ringer Avatar answered Oct 28 '22 09:10

Craig Ringer


Quick answer on OSX, set your environment variables.

>export PGHOST=localhost

>export PGPORT=5432

Or whatever you need.

like image 30
DavidLStacy Avatar answered Oct 28 '22 11:10

DavidLStacy