Reputation: 5119
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
Upvotes: 100
Views: 566249
Reputation: 11160
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...
After the editing postgresql.conf
and pg_hba.conf
you have to restart postgresql server: sudo service postgresql restart
EDIT2: Highlited configuration files.
Upvotes: 147
Reputation: 2531
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:
sudo -u postgres psql
" which should result in "postgres=#
"...\password postgres
"postgres
"Reference https://www.cherryservers.com/blog/how-to-install-and-setup-postgresql-server-on-ubuntu-20-04.
Upvotes: 0
Reputation: 9120
I’m using MacPorts and after migrating to a new machine using Apple’s Migration Assistant the db server was unavailable:
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
I tried
sudo port load postgresql14-server
but that didn’t work. Looking at the /Library/LaunchDaemons/org.macports.postgresql14-server.plist
file, though revealed two things:
<key>Disabled</key><true/>
and the actual start command
<string>--start-cmd</string>
<string>/opt/local/etc/LaunchDaemons/org.macports.postgresql14-server/postgresql14-server.wrapper</string>
So, running the start command manually finally showed the issue:
> sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql14-server/postgresql14-server.wrapper start
pg_ctl: could not open PID file "/opt/local/var/db/postgresql14/defaultdb/postmaster.pid": Permission denied
and indeed:
> sudo ls -la /opt/local/var/db/postgresql14/defaultdb/
total 60
drwx------ 26 postgres postgres 832 Jan 31 05:13 .
drwxr-xr-x 4 postgres postgres 128 Feb 5 12:31 ..
-rw------- 1 500 postgres 3 Nov 7 2021 PG_VERSION
drwx------ 22 500 postgres 704 Feb 4 10:37 base
drwx------ 60 500 postgres 1920 Feb 4 10:37 global
drwx------ 2 500 postgres 64 Nov 7 2021 pg_commit_ts
...
-rw------- 1 500 postgres 105 Jan 31 05:13 postmaster.pid
The user id was messed up. Fix:
sudo chown -hR postgres /opt/local/var/db/postgresql14/defaultdb/
to set the owner of all files to postgres
.
With that change, the server works:
> sudo /opt/local/etc/LaunchDaemons/org.macports.postgresql14-server/postgresql14-server.wrapper start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... done
server started
> psql -U postgres -W -p 5432 -h localhost
Password:
psql (14.6)
Type "help" for help.
postgres=#
\q
Upvotes: 0
Reputation: 376
If you have tried to edit pg_hba.conf and postgres.conf files as suggested above and nothing worked, then here you go !
After trying all other options suggested above, none worked for me.
So I released that this was happening due to failed PostgreSQL Cluster 14-main when booting/starting.
I followed below steps,
sudo chmod 700 -R /var/lib/postgresql/14/main
sudo -i -u postgres
/usr/lib/postgresql/14/bin/pg_ctl restart -D /var/lib/postgresql/14/main
Note: You can replace 14 with your postgresql version number, you can check /usr/lib/postgresql
/{mark the number you see here}
pg_ctl: PID file "/var/lib/postgresql/14/main/postmaster.pid" does not exist Is server running? trying to start server anyway waiting for server to start....2022-11-17 13:06:36.614 CET [5542] LOG: starting PostgreSQL 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, 64-bit 2022-11-17 13:06:36.614 CET [5542] LOG: listening on IPv4 address "0.0.0.0", port 5432 2022-11-17 13:06:36.614 CET [5542] LOG: listening on IPv6 address "::", port 5432 2022-11-17 13:06:36.616 CET [5542] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2022-11-17 13:06:36.623 CET [5543] LOG: database system was shut down at 2022-11-17 09:20:13 CET 2022-11-17 13:06:36.631 CET [5542] LOG: database system is ready to accept connections done server started
pg_lsclusters
Ver | Cluster | Port | Status | Owner | Data directory | Log file
10 main 5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
Finally, I was able to connect to PORT = 5432 with no problem and everything worked perfectly fine.
Upvotes: 5
Reputation: 27001
In my case, the server wasn't listenting because it didn't start up automatically (Windows version of PostgreSQL), because the setup program didn't create a user and didn't initialize the server. The following helped me:
After the setup I had to run (in a non-admin cmd.exe shell):
set PGDATA=C:\Program Files\PostgreSQL\14\data
cd C:\Program Files\PostgreSQL\14\bin
createuser.exe -s postgres
pg_ctl init
pg_ctl start
After that, I used the pgAdmin
tool and selected "Add new server" in the dashboard. There, I typed in the desired server name in the "General" tab, and in the "Connection" tab I entered 127.0.0.1
because I wanted to run it locally. After confirming it with Save it got created properly.
It seems that in Windows, Postgres isn't running as a Windows Service, so it doesn't start automatically and you have to run it manually. However you could create a StartPg.cmd
file and put in the command C:\Program Files\PostgreSQL\14\bin\pg_ctl start
, then you can open a Windows Explorer via Win+E, enter shell:Startup
in its path textbox (then press Enter) and copy the batch file (or a shortcut of it) into it.
Then it will start up automatically.
Upvotes: 0
Reputation: 301
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
Upvotes: 3
Reputation: 31
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.
Upvotes: 2
Reputation: 121
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
Upvotes: 9
Reputation: 21
windows === press WIN + R //open services type - services.msc
find postgres - double click on it.properties box open then click on start enjoy.
Upvotes: 1
Reputation: 339
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
listen_addresses = '*'
uncomment this line and change localhost to *
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
Upvotes: 15
Reputation: 599
If error occurred first thing first you have to read the log.
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/[email protected]
<?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/[email protected]</string>
<key>StandardOutPath</key>
<string>/usr/local/var/log/[email protected]</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.
Upvotes: 3
Reputation: 11
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
Upvotes: 0
Reputation: 628
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
Upvotes: 45
Reputation: 21
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.
Upvotes: 2
Reputation: 77
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.
Upvotes: 2
Reputation: 536
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
Upvotes: 9
Reputation: 101
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.
Upvotes: 8
Reputation: 783
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)
Upvotes: 3