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
January 30, 2025 - 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 ...
Discussions

connection to server at "127.0.0.1", port 5432 failed
So what's in the Postgres logfile? More on reddit.com
🌐 r/PostgreSQL
22
0
February 6, 2024
Can't reach database server at `postgres`:`5432`
DATABASE_URL="postgresql://postgres:postgres@postgres:5432/mydb?schema=public" ... Yours is mostly correct, except the HOST shouldn't be "postgres", but instead "localhost". More on github.com
🌐 github.com
23
9
June 28, 2022
Help me, I am sooo done
Well, for starters you can: For talk-based help (which this is) - I would switch to irc or slack - WAY more efficient ( https://www.postgresql.org/support/ ) Provide information about what OS you are talking about. How did you install PostgreSQL? How are you trying to connect (it kinda looks like you tried psql -h localhost -p 5432, but the (0x..../10061) part is NOT how normal psql behaves. Stop trusting random things you read on internet. Try to understand what is happening. We can help (irc/slack is better as it's more conversation than spread-in-time posting of comments), but the understanding has to be on your side. More on reddit.com
🌐 r/PostgreSQL
9
0
September 29, 2021
Connection to server at "localhost" (::1) failed
If you didn't manually create a new account (by your question I'm going to assume you did not), you probably meant psql -U postgres, which will use the postgres user instead More on reddit.com
🌐 r/PostgreSQL
9
2
May 15, 2024
🌐
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.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 12851363006482-Can-t-connect-to-local-Postgres-localhost-5432
Can't connect to local Postgres (localhost:5432) – IDEs Support (IntelliJ Platform) | JetBrains
August 2, 2023 - In my terminal, I'm pinging localhost fine and I can successfully telnet to 5432. I can get to the local DB using Postico as well. To try to get at this problem, I'm trying to configure a database within IDEA. It's pretty elementary. The URL is jdbc:postgresql://localhost:5432/postgres and all the other settings match my successful Postico configuration.
🌐
Medium
medium.com › @bishakhghosh0 › the-unsung-hero-of-development-localhost-5432-and-your-postgresql-database-ea7c6149b841
The Unsung Hero of Development: localhost:5432 and Your PostgreSQL Database | by Lightning Developer | Medium
August 12, 2025 - For many of us working with databases, that’s PostgreSQL on port 5432. It’s not glamorous, but it’s the backbone of countless applications, silently handling queries, transactions, and data persistence while we focus on building features. If you’ve ever typed localhost:5432 into a database client or debugged a connection issue at 2 AM, this one’s for you.
🌐
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/
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.
Find elsewhere
🌐
YouTube
youtube.com › online study for cs
How to Fix PostgreSQL Connection to Server at Localhost (::1) Port 5432 Failed Connection Refused - YouTube
How to Fix PostgreSQL Connection to Server at Localhost (::1) Port 5432 Failed Connection RefusedIn this video I have talked about How to fix PostgreSQL Conn...
Published   August 20, 2023
Views   15K
🌐
PostgreSQL
postgresql.org › docs › 9.2 › ecpg-sql-connect.html
PostgreSQL: Documentation: 9.2: CONNECT
November 9, 2017 - int main(void) { EXEC SQL BEGIN ... user name */ char *connection = "tcp:postgresql://localhost:5432/testdb"; /* connection string */ char ver[256]; /* buffer to store the version string */ EXEC ......
🌐
Localhosts
localhosts.info › 5432
localhost:5432 - PostgreSQL Database Port
Port 5432 is the standard PostgreSQL database communication port assigned by IANA.
🌐
Reddit
reddit.com › r/postgresql › connection to server at "127.0.0.1", port 5432 failed
r/PostgreSQL on Reddit: connection to server at "127.0.0.1", port 5432 failed
February 6, 2024 -

I face an error since today. Most likely as a reaction to an unexpected shut down last night (local dev.). I did not change anything regarding my setup (django <--> postgresql). Already checked out if a firewall blocked the path over night, but that seems not to be the case.

When I try to runserver:

django.db.utils.OperationalError: connection failed: could not receive data from server: Socket is not connected 

When I try to login at pgadmin:

connection to server at "127.0.0.1", port 5432 failed: Connection refused (0x0000274D/10061) Is the server running on that host and accepting TCP/IP connections?

When I try to check the db status (windows powershell):

SERVICE_NAME: postgresql-x64-14
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

There is no postmaster.pid that could couse the problem.

Can you guys help me with suggestions? I am really bad at this type of debugging and hope to retrieve some good inputs. The problem is that I do not know where to start here.

Thanks in advance

🌐
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....
🌐
PostgreSQL
jdbc.postgresql.org › documentation › use
Initializing the Driver | pgJDBC
Defaults to localhost . To specify an IPv6 address your must enclose the host parameter with square brackets, for example: jdbc:postgresql://[::1]:5740/accounting · port = The port number the server is listening on. Defaults to the PostgreSQL® standard port number (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 ...
🌐
DEV Community
dev.to › balt1794 › postgresql-port-5432-already-in-use-1pcf
PostgreSQL - port 5432 already in use - DEV Community
February 11, 2021 - In my case, I downloaded PostgreSQL, but apparently I already had a different version running on my computer and it was using the port 5432. Postgres is known for using port 5432 as a default (this can be changed).
🌐
PostgreSQL
postgresql.org › docs › current › ecpg-sql-connect.html
PostgreSQL: Documentation: 18: CONNECT
May 14, 2026 - 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 pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); EXEC SQL CONNECT TO :connection USER :user; EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); return 0; }
🌐
Reddit
reddit.com › r/postgresql › help me, i am sooo done
r/PostgreSQL on Reddit: Help me, I am sooo done
September 29, 2021 -

SOLVED} - I have multiple PSQLs installed so I needed to change the port on which this was running!

I changed it from 5432 to 5433 which I found in the .conf file PORT option, this was done with the help of u/depesz on the slack channel for postgres

Unable to connect to server:

could not connect to server: Connection refused (0x0000274D/10061)Is the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 5432?could not connect to server: Connection refused (0x0000274D/10061)Is the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 5432?

I have basically tried everythinjg thing I have read on the internet!

what the do I do?