You have to edit postgresql.conf file and change the listen_addresses line.

This file you can find in the /etc/postgresql/9.3/main directory.

Default Ubuntu config have allowed only localhost (or 127.0.0.1) interface, which is sufficient for using, when every PostgreSQL client work on the same computer, as PostgreSQL server. If you want connect PostgreSQL server from other computers, you have change this config line in this way:

listen_addresses = '*'

Then you have edit pg_hba.conf file, too. In this file you have set, from which computers you can connect to this server and what method of authentication you can use. Usually you will need similar line:

host    all         all         192.168.1.0/24        md5

Please, read comments in this file...

EDIT:

After the editing postgresql.conf and pg_hba.conf you have to restart postgresql server: sudo service postgresql restart

EDIT2: Highlited configuration files.

Answer from Jan Marek on Stack Overflow
🌐
Pinggy
pinggy.io › know_your_port › localhost_5432
localhost:5432 - PostgreSQL Database Port Guide - Pinggy
March 7, 2026 - Localhost:5432 is the official default port for PostgreSQL, the world’s most advanced open-source relational database. “Localhost” refers to your own computer (typically mapped to IP address 127.0.0.1), and “5432” is the port number ...
🌐
PostgreSQL
postgresql.org › docs › 9.2 › ecpg-sql-connect.html
PostgreSQL: Documentation: 9.2: CONNECT
November 9, 2017 - int main(void) { EXEC SQL BEGIN DECLARE SECTION; char *dbname = "testdb"; /* database name */ char *user = "testuser"; /* connection user name */ char *connection = "tcp:postgresql://localhost:5432/testdb"; /* connection string */ char ver[256]; /* buffer to store the version string */ EXEC SQL END DECLARE SECTION; ECPGdebug(1, stderr); EXEC SQL CONNECT TO :dbname USER :user; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); EXEC SQL CONNECT TO :connection USER :user; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); return 0; } CONNECT is specified in the SQL standard, but the format of the connection parameters is implementation-specific.
🌐
PostgreSQL
postgresql.org › docs › current › runtime-config-connection.html
PostgreSQL: Documentation: 18: 19.3. Connections and Authentication
May 14, 2026 - While client authentication (Chapter ... interfaces. This parameter can only be set at server start. ... The TCP port the server listens on; 5432 by default....
Top answer
1 of 16
105

This issue comes from installing the postgres package without a version number. Although postgres will be installed and it will be the correct version, the script to setup the cluster will not run correctly; it's a packaging issue.

If you're comfortable with postgres there is a script you can run to create this cluster and get postgres running. However, there's an easier way.

First purge the old postgres install, which will remove everything of the old installation, including databases, so back up your databases first.. The issue currently lies with 9.1 so I will assume that's what you have installed

sudo apt-get remove --purge postgresql-9.1

Now simply reinstall

sudo apt-get install postgresql-9.1

Note the package name with the version number. HTH.

2 of 16
38

The error message refers to a Unix-domain socket, so you need to tweak your netstat invocation to not exclude them. So try it without the option -t:

netstat -nlp | grep 5432

I would guess that the server is actually listening on the socket /tmp/.s.PGSQL.5432 rather than the /var/run/postgresql/.s.PGSQL.5432 that your client is attempting to connect to. This is a typical problem when using hand-compiled or third-party PostgreSQL packages on Debian or Ubuntu, because the source default for the Unix-domain socket directory is /tmp but the Debian packaging changes it to /var/run/postgresql.

Possible workarounds:

  • Use the clients supplied by your third-party package (call /opt/djangostack-1.3-0/postgresql/bin/psql). Possibly uninstall the Ubuntu-supplied packages altogether (might be difficult because of other reverse dependencies).
  • Fix the socket directory of the third-party package to be compatible with Debian/Ubuntu.
  • Use -H localhost to connect via TCP/IP instead.
  • Use -h /tmp or equivalent PGHOST setting to point to the right directory.
  • Don't use third-party packages.
🌐
CBT Nuggets
cbtnuggets.com › common-ports › what-is-port-5432
What is Port 5432?
October 11, 2024 - This is because TCP, unlike User Datagram Protocol (UDP), establishes the kind of reliable connections necessary for database applications. PostgreSQL uses port 5432 to facilitate connections between PostgreSQL clients and servers.
🌐
Localhosts
localhosts.info › 5432
localhost:5432 - PostgreSQL Database Port
Port 5432 is the default PostgreSQL database server port. Connect to PostgreSQL, fix connection refused errors, configure pg_hba.conf, and manage Postgres databases.
Find elsewhere
🌐
Project-open
project-open.com › en › howto-postgresql-port-secure-remote-access
HowTo Safely Open a PostgreSQL Port for Remote Access?
#------------------------------------------------------------------------------ # CONNECTIONS AND AUTHENTICATION #------------------------------------------------------------------------------ # - Connection Settings - listen_addresses = '0.0.0.0' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) port = 5432 # (change requires restart) After that we need to restart PG to activate the changes. On recent Linux system you have to enter as user root: ... On Windows you can use Control Panel -> Administrative Tools -> Services and restart the PostgreSQL service.
🌐
DEV Community
dev.to › lightningdev123 › localhost5432-your-gateway-to-postgresql-ehb
localhost:5432 - Your Gateway to PostgreSQL - DEV Community
August 11, 2025 - Localhost → This is just your own computer, usually mapped to the IP address 127.0.0.1. 5432 → This is the default port number where PostgreSQL listens for incoming connections.
🌐
PostgreSQL
postgresql.org › message-id › CALyyT7TKt8kgBU2qOhLFvAS11YNbrSq=Vsgj-02hBa+0KNQ88g@mail.gmail.com
PostgreSQL: Re: cant connect to localhost:5432 (but unix socket ok)
February 4, 2021 - > > I could have done something ... I am not fully aware of. > > There are couple of potential issues: > 1. it might listen on different port than 5432....
🌐
Hacker News
news.ycombinator.com › item
>By default, PostgreSQL listens on a TCP port 5432. This post seems to outright ... | Hacker News
April 3, 2021 - This post seems to outright state that by default postgres is listening to everyone via TCP for connection · Unless you edit pg_ident.conf, your postgres install will not listen for connections outside of on localhost. So, while it's correct to say that it listens to TCP port 5432, there is ...
Top answer
1 of 7
245

/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.

2 of 7
30

Quick answer on OSX, set your environment variables.

>export PGHOST=localhost

>export PGPORT=5432

Or whatever you need.

🌐
Stream Security
stream.security › rules › ensure-there-is-no-unrestricted-inbound-access-to-tcp-port-5432-postgresql
Security & Compliance | Ensure there is no unrestricted inbound access to TCP port 5432 (PostgreSQL)
TCP port 5432 is the default port used by PostgreSQL, an open source relational database management system. It is used for client-server communication and allows clients to connect to the PostgreSQL server.Ensuring that there is no unrestricted inbound access to TCP port 5432 is important to ...
🌐
PortChecker
check-port.com › home › port info › port 5432
Port 5432 — PostgreSQL: What It Is, Uses & Security Guide
Port 5432 is the default TCP port for PostgreSQL. It is used by servers and applications that need to accept incoming connections for this service.
🌐
Ask Ubuntu
askubuntu.com › questions › 1498234 › psql-connection-to-server-at-localhost-127-0-0-1-port-5432-failed-connect
postgresql - psql: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused - Ask Ubuntu
December 27, 2023 - I checked a lot on the Internet to fix the problem but couldn't do anything. First of all, I changed my configuration based on here. listen_addresses = '*' port= 5432 data_directory = '/var/lib/
🌐
Broadcom
knowledge.broadcom.com › external › article › 264811 › postgresql-does-not-accept-tcpip-connect.html
PostgreSQL does not accept TCP/IP connections.
June 3, 2026 - could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? ... Configuration. By default, PostgreSQL does not allow remote connections.
🌐
Oracle DBA Training
learnomate.org › home › postgresql › psql: could not connect to server: connection refused
7 Proven Fixes for PostgreSQL Connection Refused Error (Quick Solutions)
September 11, 2025 - One of the most frustrating issues for DBAs and developers is the PostgreSQL connection refused error. The full message usually looks like this: psql: could not connect to server: Connection refused Is the server running on host "localhost" and accepting TCP/IP connections on port 5432?