ChatGPT解决这个技术问题 Extra ChatGPT

Can't connect to Postgresql on port 5432

I have PostgreSQL 9.3 installed on a server running Ubuntu Server 14.04.

If I ssh into the server via terminal, I'm able to connect with psql. But when I try to configure pgAdmin III to do the remote connection, I get:

Server doesn't listen The server doesn't accept connections: the connection library reports could not connect to server: Connection refused Is the server running on host "172.24.3.147" and accepting TCP/IP connections on port 5432?

When I run on the server service postgresql status it gives me:

9.3/main (port 5432): online

So of course I'm missing something important here.

EDIT

When running netstat -na on the server, I get (relevant portion, I guess):

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN     
tcp        0      0 172.24.3.147:22         172.24.3.240:61950      ESTABLISHED
tcp        0      0 172.24.3.147:22         172.24.3.240:60214      ESTABLISHED
you didn't bind to that ip. pg's listening on something else, like maybe just 127.0.0.1:5432
What do you mean 'bind'? I'm using the port of the server (used in ssh), when I try to connect via pgAdmin.
you have to tell pg what interface/ip to listen to for connections. if it bound to only 127.0.0.1:5432, then any attempts on 172.24.3.147:5432 won't work, because nothing is listening for that ip:port combo
Per your comment, it should be reading on port 22 (why, if the default is 5432?), right? (That's what's in the State=ESTABLISHED line above). But when I try to connect to that port on pgAdmin, I get a different error: "An error has occurred: 14:57:13: Error: Error connecting to the server: SSL error: unknown protocol expected authentication request from server, but received S"
port 22 is ssh, and has nothing to do with postgres.

J
Jan Marek

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

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.

EDIT2: Highlited configuration files.


Is "192.168.1.0" the remote server connecting to the Postgres DB? Why port 24?
@ScottSkiles 192.168.1.0 is address of network, /24 is not a number of port, but length of network mask in bites (255.255.255.0). In the other words: Allowed are computers from network 192.168.1.0/255.255.255.0, having addresses from 192.168.1.1 to 192.168.1.254
So a single IP would be (for 192.168.1.1): 192.168.1.1/1?
@ScottSkiles single ip would be 192.168.1.1/32
Which file are you talking about? There are at least 5 conf files.
a
akshay gore

Uncomment the listen_addresses = '*' in the postgresql.conf

This has bitten me a second time so I thought might be worth mentioning. The line listen_addresses = '*' in the postgresql.conf is by default commented. Be sure to uncomment (remove the pound sign, # at the beginning) it after updating otherwise, remote connections will continue to be blocked.

PS: psql -U postgres -c 'SHOW config_file' - to locate the postgresql.conf file path


It took my whole day. Thanks so much!
m
max56

Had same problem with psql via command line connecting and pgAdmin not connecting on RDS with AWS. I did have my RDS set to Publicly Accessible. I made sure my ACL and security groups were wide open and still problem so, I did the following: sudo find . -name *.conf then sudo nano ./data/pg_hba.conf then added to top of directives in pg_hba.conf file host all all 0.0.0.0/0 md5 and pgAdmin automatically logged me in.

This also worked in pg_hba.conf file host all all md5 without any IP address and this also worked with my IP address host all all <myip>/32 md5

As a side note, my RDS was in my default VPC. I had an identical RDS instance in my non-default VPC with identical security group, ACL and security group settings to my default VPC and I could not get it to work. Not sure why but, that's for another day.


E
Eric Aya

Remember to check firewall settings as well. after checking and double-checking my pg_hba.conf and postgres.conf files I finally found out that my firewall was overriding everything and therefore blocking connections


r
rtfmpliz

just summary from the accepted answer

if maybe someone like me overlook the filename that must be edit

in my case the conf file located at

/etc/postgresql/14/main'

if you type cd /etc/postgresql/14/main

so just sudo nano add these 2 filenames

postgresql.conf

listen_addresses = '*'

uncomment this line and change localhost to *

pg_hba.conf

host all all 0.0.0.0/0

change the 127.0.0.1/32 change to 0.0.0.0/0

last don't forget to restart using sudo service postgresql restart

I hope this one clears the notification error


doesnt help with my connection refused...
D
Dinesh Penugonda

You have to edit pg_hba.conf to accept all requests within the network

#TYPE  DATABASE        USER            ADDRESS                 METHOD 
host    all             all             0.0.0.0/0                md5
host    all             all             ::1/128                  md5

From your application, you can connect using this IP address like below example: -

postgresql://postgres:******@192.168.1.101:5432/app


Only this one has worked for me. Thanks!
D
Daniel Kravetz Malabud

You probably need to either open up the port to access it in your LAN (or outside of it) or bind the network address to the port (make PostgreSQL listen on your LAN instead of just on localhost)


Sorry, as a lifetime PC developer, I'm not entirely familiar with this port thing. Right now I only need to access it inside our LAN (a proxy-based institutional network). The remaining of your message is unintelligible to me. See the other comments, please.
The answer by Jan Marek should be what you are looking for. tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN this line shows that Postgre's only listening for connection coming from the same computer where postgre is running, and not from the local network.
All right. But how do you guys know that the selected line is about Postgres? Because 5432 is their default port number?
Correct. It only shows two ports, 22 which is the default one for SSH, and you already mentioned that you are using SSH, and 5432 which is the default one for Postgres, which you mentioned that you are using and having issues with.
A
Adrian Stanica

I had the same problem after a MacOS system upgrade. Solved it by upgrading the postgres with brew. Details: it looks like the system was trying to access Postgres 11 using older Postgres 10 settings. I'm sure it was my mistake somewhere in the past, but luckily it all got sorted out with the upgrade above.


S
Scott Moore

I had this same issue. I originally installed version 10 because that was the default install with Ubuntu 18.04. I later upgraded to 13.2 because I wanted the latest version. I made all the config modifications, but it was still just binging to 1207.0.0.1 and then I thought - maybe it is looking at the config files for version 10. I modified those and restarted the postgres service. Bingo! It was binding to 0.0.0.0

I will need to completely remove 10 and ensure that I am forcing the service to run under version 13.2, so if you upgraded from another version, try updating the other config files in that older directory.


Had the same issue as well. I had upgraded my version of PostgreSQL and turns out editing the old version's config was the way to go.
O
OZLEM CITCI

I use home-brew to start my server.

1)stop the server (but it is not working. it Is the problem)

brew services stop postgresql

2)make update if it is needed

brew postgresql-upgrade-database

3)below comment solve my problem it start the server. Thats it

brew services start postgresql


d
drosanda

If error occurred first thing first you have to read the log.

This only applicable for postgresql installation through brew on your mac *

First, you have to check the postgresql status by running

brew services on your terminal

If its stopped, try to started it and then run the brew services again

And if the status show error, you can easily find the log location through the *.plist file.

Example, on my configuration

cat /Users/drosanda/Library/LaunchAgents/homebrew.mxcl.postgresql@12.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>homebrew.mxcl.postgresql@12</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/opt/postgresql@12/bin/postgres</string>
        <string>-D</string>
        <string>/usr/local/var/postgresql@12</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/postgresql@12.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/postgresql@12.log</string>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
</dict>
</plist>

Here is the log file content:

2021-11-30 09:07:20.154 WIB [3891] FATAL: lock file "postmaster.pid" already exists

2021-11-30 09:07:20.154 WIB [3891] HINT: Is another postmaster (PID 422) running in data directory "/usr/local/var/postgresql@12"?

For my case, the postgresql won't start caused by the PID file is still exists.

It's easy to fix, by removed the postmaster.pid file.

Just add an answer that might useful for someone later.


j
joy

windows === press WIN + R //open services type - services.msc find postgres - double click on it. properties box open then click on start enjoy.


As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
J
Johnny D

adding an updated answer for another possibility. running postgres 10 on AWS EC2 I had to add a custom inbound rule through Windows Defender Firewall to allow port 5432.


S
Swiss bobo

For anyone else coming from docker and other applications issues like node js or Django or anything

i faced this problem for 2 days or more watched videos tutorials nothing seem to work although ill say i have missed some but here is the solution that worked for me

instead of local host for PGHOST

you must specify your service name

e.g

    image: postgres
    container_name: postgresdb
    restart: always
    env_file:
      - ./env/.env
    volumes:

then your host must be PGHOST:nodeapp-db


K
Kmb40

Had this same issue and searches resulted in many complex overly solutions. Ultimately this issue was that a password had not been set after Postgres was installed on Ubuntu.

Solution:

From the Postgres terminal - reached by entering "sudo -u postgres psql" which should result in "postgres=#"... Set the password using "\password postgres" Update PG Admin with the new password and the username "postgres"

Reference https://www.cherryservers.com/blog/how-to-install-and-setup-postgresql-server-on-ubuntu-20-04.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now