问题
I typed psql and I get this:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I used sudo netstat -nlp | grep 5432 to see the status but nothing showed.
And I searched online, somebody told me to modify pg_hba.conf but I can't locate this file. And I also tried this commandsudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432. It can't work.
回答1:
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.
回答2:
If there is no error in starting the postgres service, follow these steps
Step 1: Running pg_lsclusters will list all the postgres clusters running on your device
eg:
Ver Cluster Port Status Owner Data directory Log file
9.6 main 5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
most probably the status will be down in your case and postgres service
Step 2: Restart the pg_ctlcluster
#format is pg_ctlcluster <version> <cluster> <action>
sudo pg_ctlcluster 9.6 main start
#restart postgres
sudo service postgres restart
Step 3: Step 2 failed and threw error
If this process is not successfull it will throw the error.
My error was(You can see the error log on /var/log/postgresql/postgresql-9.6-main.log)
FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to the group `ssl-cert`
Step 4: check ownership of postgres
Make sure that postgres is the owner of /var/lib/postgresql/version_no/main
eg: sudo chown postgres -R /var/lib/postgresql/9.6/main/
Step 5: Check postgres user belongs to ssl-cert user group
It happened to me and it turned out that I removed erroneously the Postgres user from "ssl-cert" group. Run the below code to fix the user group issue and fixing the permissions
#set user to group back with
sudo gpasswd -a postgres ssl-cert
# Fixed ownership and mode
sudo chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key
# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgres restart
回答3:
I have encountered a similar issue a couple of times. Normally I just do a fresh installation of PostgreSQL following this tutorial and that solves the problem at the expense of losing data.
I was determined on getting real fix today. Restarting PostgreSQL resolved it on ubuntu. sudo /etc/init.d/postgresql restart
回答4:
Solved it! Although I don't know what happened, but I just deleted all the stuff and reinstalled it. This is the command I used to delete it sudo apt-get --purge remove postgresql\* and dpkg -l | grep postgres. The latter one is to find all the packets in case it is not clean.
回答5:
I was facing same problem and
sudo su - postgres
initdb --locale en_US.UTF-8 -D /var/lib/postgres/data
exit
sudo systemctl start postgresql
sudo systemctl status postgresql
This worked for me.
回答6:
quick howto on debian:
- edit
/etc/postgresql/10/main/postgresql.confwith listen_address * - edit
/etc/postgresql/10/main/pg_hba.confand add line in the end withhost all all 0/0 md5 - create login role
postgres=# CREATE ROLE remoteuser LOGIN WITH PASSWORD 'foo' sudo /etc/init.d/postgresql restartchanges take effectlogin from clientside with
psql --host=ipofserver --port=5432 --username=remoteuser --password --dbname=mydb- the password is interactivly asked which in this case is foo
thats how to remotely access postgres database on server from the psql client
回答7:
The error means that the Postgres server is not running. Try starting it:
sudo systemctl start postgresql
Make sure that the server starts on boot:
sudo systemctl enable postgresql
回答8:
I had the same issue on Devuan ascii (maybe Debian, too?). The config file /etc/postgresql/9.6/main/postgresql.conf contains a directive unix_socket_directories which points to /var/run/postgresql by default. Changing it to /tmp, where most clients look by default, fixed it for me.
回答9:
Just want to make a small addition: if your instance is complaining on a socket, you can also check unix_socket_directories at /data/postgresql.conf file which could have been set to /tmp, for example, if you have used a 3rd party distribution. You can change it to /var/run/postgresql and restart the service. That may also require creating a postgresql dir at /var/run and subsys/postgresql-9.6 at /var/lock if those doesn't already exist (worked for me with postgresql 9.6).
回答10:
My issue with this error message was in wrong permissions on key and pem certificates, which I have manipulated. What helped me a lot was: /var/log/postgresql/postgresql-9.5-main.log where are all the errors.
回答11:
So for me and my pals working on a Node.js app (with Postgres and Sequelize), we had to
brew install postgresql(one of us was missing postgres, one of us was not, and yet we were getting the same error msg as listed above)brew services start postgresql**** (utilize Homebrew to start postgres)createdb <name of database in config.json file>node_modules/.bin/sequelize db:migratenpm start
回答12:
During fresh installation of postgresql. By default, user name and password is assigned as "postgres". The feature this RDBMS provides is to add role for new user and create database. If you are getting such errors:
login in by default username:
root@kalilinux:~# sudo -i -u postgres
ype psql for interactive prompt
postgres@kalilinux:~$ psql
To quit from prompt use
\q
To create new user role
postgres@kalilinux:~$ createuser --interactive
Now you are in interacive psql shell. Enjoy. Dont forget to login in from your username and type psql for shell.
回答13:
It can cause anything for example, my issue was caused for typo error on configuration files. Some of people says caused by certificate files, another group says caused by unmatched locals.
If you cant find any solution about your issue, remove postgres and reinstall it.This is the best solution.
回答14:
I resolved this problem by checking my file system the disk was completely full, and so database could not start up
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432" ?
I tried series of troubleshooting, until when i checked my disk usage and found that it was full, 100% usage,
df -h
cd /var/log/odoo/
cat /dev/null > odoo-server.log
reboot
回答15:
I had the same problem. It seems that there is no socket when there is no cluster.
The default cluster creation failed during the installation because no default locale was set.
回答16:
I had the similar issue and the problem was in the config file pg_hba.conf. I had earlier made some changes which was causing the server to error out while trying to start it. Commenting out the extra additions solved the problem.
回答17:
Restarting the instance worked for me. Also, as mentioned in some other post psql -h '/tmp' worked as well before the restart. But post restart psql directly started working. So, probably some file ownership issues that got reset with the restart is what I am thinking.
回答18:
If your service is not secure, this may be the reason
vi /etc/postgresql/11/main/pg_hba.conf
- open hba config file, this config file usualy located in the etc directory.
host all all localhost trust md5
you can remove the trust keyword
save pg_hba.conf
- sudo service postgresql restart.
回答19:
Verify that Postgres is running using:
ps -ef | grep postgres
root@959dca34cc6d:/var/lib/edb# ps -ef|grep postgres
enterpr+ 476 1 0 06:38 ? 00:00:00 /usr/lib/edb-as/11/bin/edb-postgres -D /var/lib/edb-as/11/main2 -c config_file=/etc/edb-as/11/main2/postgresql.conf
Check for data directory and postgresql.conf.
In my case data directory in -D was different than that in postgresql.conf
So I changed the data directory in postgresql.conf and it worked.
来源:https://stackoverflow.com/questions/31645550/why-psql-cant-connect-to-server