HOWTO · PostgreSQL
PostgreSQL Default Port
PostgreSQL uses port 5432 by default. Learn how to identify the instance listening, connect on a chosen port, change its configuration, and diagnose connection mismatches.
On this page
PostgreSQL’s default port is 5432. This is the default used by PostgreSQL clients and server configuration; it does not prove that a server is running on your computer, listening on that port, or reachable from your client. A package, administrator, or deployment may configure another port.
What Is PostgreSQL’s Default Port?
Port 5432 is the conventional default for PostgreSQL connections over TCP/IP. A client needs the server’s host and port, as well as valid database and role credentials. localhost means the same computer as the client, so use it only when the server is local. The database name, role, password, and host are installation-specific; there is no universal test account.
A port identifies a network endpoint on a host. It is distinct from a Unix-domain socket, which connects through a local filesystem path rather than a TCP host-and-port pair. The two connection methods can reach different PostgreSQL instances, even when they are used from the same computer.
Check Which PostgreSQL Instance Is Listening
Connect to the instance you intend to check, then ask it for its identity and settings. In a psql session, run SELECT version();, SHOW data_directory;, SHOW config_file;, and SHOW port;. The results identify the server version, its data directory, the configuration file it reads, and its configured port. These are server-reported values, so they help distinguish instances when several are installed or running.
The client program and the server are separate: finding a psql executable on your PATH does not establish which server it reaches. If a command connects to an unexpected instance, check which psql executable your shell selects and explicitly provide the intended host and port. On Unix-like systems, omitting -h can select a local socket; -h localhost requests a TCP connection. The socket directory is controlled by the selected server’s unix_socket_directories setting.
Connect to PostgreSQL on a Specific Port
For a local server, use psql -h localhost -p 5432 -U your_role -d your_database. Replace the role and database placeholders with values configured for your instance. To try another configured port, such as 5433, change the value after -p; that example does not imply another server is available there. For a remote server, replace localhost with its hostname or address and ensure the server is configured to accept connections from your client.
The following Python snippet assembles a client command for a local server on its default port. It displays the command but does not start psql or connect to a database. Run the displayed command in a shell after confirming the client is installed and the connection details match your server.
import shlex
host = "localhost"
port = 5432
database = "my_database"
role = "my_role"
command = ["psql", "-h", host, "-p", str(port), "-U", role, "-d", database]
print(shlex.join(command))
Change PostgreSQL’s Listening Port
First connect to the intended instance and run SHOW config_file; to locate the configuration file that it actually uses. Inspect that file for its port setting; package-specific paths vary, so do not assume a path based only on the operating system. Set the value to the port you want, then restart that PostgreSQL instance for the listening-port change to take effect. Check the service manager or deployment instructions for how that instance is restarted.
After the restart, connect with psql using the new port and run SHOW port; to confirm the server’s configured value. If another process already occupies the chosen port, PostgreSQL may fail to bind and start listening. Choose a free port or resolve the conflict, and check the server’s startup logs for the specific failure. Do not assume an installer will select a replacement port automatically.
Troubleshoot PostgreSQL Port and Connection Problems
If a connection fails or reaches the wrong server, check these items in order:
- Host and port: Confirm that the client uses the intended host and the port configured for that server. Port 5432 is only the default; deployments can use a different value.
- Instance identity: If the connection succeeds but shows unexpected data, run
SELECT version();,SHOW data_directory;, andSHOW port;in that session. Compare the results with the intended instance. - Connection method: Check whether the client uses TCP/IP or a local socket. Use
-h localhostto request TCP on the local computer; for a socket connection, confirm the selected instance’s socket directory. - Listening and access settings: Confirm that the server is running, can bind to the selected port, and is configured to accept connections on the requested network interface. Remote access also depends on the server’s access rules and network controls.
- Client and server details: Make sure the selected
psqlclient is the one you intend to use, and supply the database, role, and authentication settings configured for the instance.
Operating-system commands for finding a process that owns a port differ, so use the listener-inspection tool documented for your platform. The result can identify a process, but the SQL checks above are still useful for confirming which PostgreSQL instance accepted a connection.