Is the server running on host localhost (::1) and accepting TCP/IP connections on port 5432?
You most likely ran out of battery and your postgresql server didn't shutdown correctly.
The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)
could not connect to server: Connection refused Is the server running on host ... and accepting TCP/IP connections on port 5432?
Check listen_addresses in postgresql.conf file allowed remote connections. It should be
listen_addresses = '*'
In pg_hba.conf file you need to add following entry
host all all 0.0.0.0/0 md5
Be sure it is not a firewall issue.
Can't connect to Postgresql on port 5432
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.
psql: could not connect to server: Connection refused
It's hard to give a canonical answer but here are a few things to try (some of them may not apply / be possible):
- Ping the IP address in question - is the server up at all / reachable? Are there other services on the box that can be reached?
- Connect from a local connection on the server itself, assuming you have console or ssh access
- If you have local access and network based access doesn't work, is unix-domain socket access allowed and if so, does that work?
- Check the port config in postgresql.conf - is it really where you think it is?
- Has the config file been edited since the last server restart? The parameters you listed all require server restarts to take effect.
- Is it actually using the config file you think it is? Running "SHOW config_file;" as superuser will help if you can make a local connection
- What happens is you do telnet 1486? Do you get a network connection or similar error?
- Get a pcap and check what's happening at the network level (wireshark or tcpdump will help)
Postgres could not connect to server
Had a similar problem; a pid file was blocking postgres from starting up. To fix it:
$ rm /usr/local/var/postgres/postmaster.pid
$ brew services restart postgresql
and then all is well.
UPDATE:
For Apple M1 (Big Sur) users, do this instead:
$ rm /opt/homebrew/var/postgres/postmaster.pid
$ brew services restart postgresql
psql: could not connect to server: Connection refused Error when connecting to remote database
cd /etc/postgresql/9.x/main/
open file named postgresql.conf
sudo vi postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
PG::ConnectionBad - could not connect to server: Connection refused
It could be as simple as a stale PID file. It could be failing silently because your computer didn't complete the shutdown process completely which means postgres didn't delete the PID (process id) file.
The PID file is used by postgres to make sure only one instance of the server is running at a time. So when it goes to start again, it fails because there is already a PID file which tells postgres that another instance of the server was started (even though it isn't running, it just didn't get to shutdown and delete the PID).
- To fix it remove/rename the PID file. Find the postgres data directory. On macOS using homebrew it is in
/usr/local/var/postgres/
,
or/usr/local/var/log/
other systems it might be/usr/var/postgres/
. On M1, it might be/opt/homebrew/var/postgresql
. - To make sure this is the problem, look at the log file (
server.log
). On the last lines you will see:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
If so,
rm postmaster.pid
Restart your server. On a mac using launchctl (with homebrew) the following commands will restart the server.
brew services restart postgresql
OR on older versions of Brew
launchctl unload homebrew.mxcl.postgresql.plist
launchctl load -w homebrew.mxcl.postgresql.plist
Related Topics
What Do the Fields of Ruby's Gc.Stat Mean
Find Memory Leak in a Ruby on Rails Project
Sinatra - Response.Set_Cookie Doesn't Work
How to Setup Urls for Static Site with Ruby Rack on Heroku
Using Ruby, Reading a File, Containing Name/Value Pairs into a Hash
Heroku App Fails to Start - 'Require': No Such File to Load -- Sinatratestapp (Loaderror)
Rake Db:Migrate Error with MySQL2 Gem - Library Not Loaded: Libssl.1.0.0.Dylib
Stylesheet_Link_Tag :All Versus :Media =>All
How to Use Rspec Expectations in Irb
Adding Keywords to Ruby Syntax Highlighting for Notepad++
Rails 3: Call Functions Inside Controllers
How to Look Up Elevation Data by Lat/Lng
Active Admin Scopes for Each Instance of a Related Model
Do Ruby 1.8 and 1.9 Have the Same Hash Code for a String
How to Update Gems in Ruby for Windows