PostgreSQL: Why psql can't connect to server?
The error states that the psql utility can't find the socket to connect to your database server. Either you don't have the database service running in the background, or the socket is located elsewhere, or perhaps the pg_hba.conf
needs to be fixed.
Step 1: Verify that the database is running
The command may vary depending on your operating system. But on most *ix systems the following would work, it will search for postgres among all running processes
ps -ef | grep postgres
On my system, mac osx, this spits out
501 408 1 0 2Jul15 ?? 0:21.63 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
The last column shows the command used to start the server, and the options.
You can look at all the options available to start the postgres server using the following.
man postgres
From there, you'd see that the options -D
and -r
are respectively the datadir
& the logfilename
.
Step 2: If the postgres service is running
Use find
to search for the location of the socket, which should be somewhere in the /tmp
sudo find /tmp/ -name .s.PGSQL.5432
If postgres is running and accepting socket connections, the above should tell you the location of the socket. On my machine, it turned out to be:
/tmp/.s.PGSQL.5432
Then, try connecting via psql using this file's location explicitly, eg.
psql -h /tmp/ dbname
Step 3: If the service is running but you don't see a socket
If you can't find the socket, but see that the service is running, Verify that the pg_hba.conf file allows local sockets.
Browse to the datadir
and you should find the pg_hba.conf
file.
By default, near the bottom of the file you should see the following lines:
# "local" is for Unix domain socket connections only
local all all trust
If you don't see it, you can modify the file, and restart the postgres service.
Permission denied when connecting to server Postgres
After dealing with this problem for days I finally managed to solve it by uninstalling and installing Postgresql via Homebrew and initiating the server. This is how I did it:
make sure Xcode (for Os X users) is up to date
xcode-select --install
Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew search
brew install postgres
brew update
Postgres should be install. Now, make sure you have the permissions required to use it, this is done by allowing your user such permission:
sudo chown -R :admin /usr/local
The permission problem should be solved. Now you (as I did) could be facing a problem related to the server.
psql -U postgres -p 5432 -h localhost
This will do the trick:
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres-deletemewhenitsallgood
initdb /usr/local/var/postgres -E utf8
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
or
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
You can now start the database server using:
pg_ctl -D /usr/local/var/postgres -l logfile start
Don't forget to adapt this command lines to your specific case and addresses.
Good luck
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
django could not connect to server: Permission denied Is the server running on host and accepting TCP/IP connections on port remote host
Based on your description this has something to do with the Apache and it's permissions to access the database. You didn't mention which OS you use but if it is running SELinux the default rules prevent apache from connecting to db. If this is the case you can temporarily enable it by running:
setsebool httpd_can_network_connect_db
Here are more detailed instructions.
OperationalError: could not connect to server: Permission denied tTCP/IP connections on port 5432?
Actually I have enabled selinux httpd_can_network_connect_db parameters on db server instead of web server
So issue got solved after enabling httpd_can_network_connect_db on web server
Related Topics
How to Make Binary Distribution of Qt Application for Linux
Is /Dev/Random Considered Truly Random
Hadoop: Require Root's Password After Enter "Start-All.Sh"
Can Gdb Change the Assembly Code of a Running Program
Scripts Launched from Udev Do Not Have Display Access Anymore
How to Strip Path While Archiving with Tar
Use Gnuplot to Plot SQLite Database
Finding Contents of One File in Another File
Docker Alpine Executable Binary Not Found Even If in Path
How to Add Chromedriver to Path in Linux
Get Parent Directory of a File in Bash
Linux Kernel - Add System Call Dynamically Through Module
Can Inode and Crtime Be Used as a Unique File Identifier
Tcp: Server Sends [Rst, Ack] Immediately After Receiving [Syn] from Client
Get Utf-8 Input with X11 Display
How to Call a Function (Defined in Shell Script) in a Perl Script