PostgreSQL: Why psql can't connect to server?

前端 未结 22 2135
情书的邮戳
情书的邮戳 2020-12-07 10:40

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
    con         


        
相关标签:
22条回答
  • I experienced this issue when working with PostgreSQL on Ubuntu 18.04.

    I checked my PostgreSQL status and realized that it was running fine using:

    sudo systemctl status postgresql
    

    I also tried restarting the PotgreSQL server on the machine using:

    sudo systemctl restart postgresql
    

    but the issue persisted:

    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"?
    

    Following Noushad' answer I did the following:

    List all the Postgres clusters running on your device:

    pg_lsclusters
    

    this gave me this output in red colour, showing that they were all down and the status also showed down:

    Ver Cluster Port Status Owner    Data directory              Log file
    10  main    5432 down   postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
    11  main    5433 down   postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
    12  main    5434 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
    

    Restart the pg_ctlcluster for one of the server clusters. For me I restarted PG 10:

    sudo pg_ctlcluster 10 main start
    

    It however threw the error below, and the same error occurred when I tried restarting other PG clusters:

    Job for postgresql@10-main.service failed because the service did not take the steps required by its unit configuration.
    See "systemctl status postgresql@10-main.service" and "journalctl -xe" for details.
    

    Check the log for errors, in this case mine is PG 10:

    sudo nano /var/log/postgresql/postgresql-10-main.log
    

    I saw the following error:

    2020-09-29 02:27:06.445 WAT [25041] FATAL:  data directory "/var/lib/postgresql/10/main" has group or world access
    2020-09-29 02:27:06.445 WAT [25041] DETAIL:  Permissions should be u=rwx (0700).
    pg_ctl: could not start server
    Examine the log output.
    

    This was caused because I made changes to the file permissions for the PostgreSQL data directory.

    I fixed it by running the command below. I ran the command for the 3 PG clusters on my machine:

    sudo chmod -R 0700 /var/lib/postgresql/10/main
    sudo chmod -R 0700 /var/lib/postgresql/11/main
    sudo chmod -R 0700 /var/lib/postgresql/12/main
    

    Afterwhich I restarted each of the PG clusters:

    sudo pg_ctlcluster 10 main start
    sudo pg_ctlcluster 11 main start
    sudo pg_ctlcluster 12 main start
    

    And then finally I checked the health of clusters again:

    pg_lsclusters
    

    this time around everything was fine again as the status showed online:

    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
    11  main    5433 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
    12  main    5434 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
    

    That's all.

    I hope this helps

    0 讨论(0)
  • 2020-12-07 11:07

    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.

    0 讨论(0)
  • 2020-12-07 11:08

    In my case, the service was running but the cluster was down and psql wouldn't start. My configuration files looked perfect but it kept throwing configuration errors and seemed to ignore the changes I was making.

    It turns out that whenever you use ALTER SYSTEM SET ... syntax, PostgreSQL writes to a file called postgresql.auto.conf. That file is read in addition to the regular postgresql.conf and pg_hba.conf files. In my distribution of Ubuntu (18.04), they are in different folders(!):
    - pg_hba.conf and postgresql.conf are both in /etc/postgresql/12/main
    - The auto-generated file is /var/lib/postgresql/12/main/postgresql.auto.conf

    I had tried to change the configuration using ALTER SYSTEM SET listen_addresses = <my-ip>, but had made a mistake and that created a broken "ghost" configuration that I couldn't find. As soon as I erased the offending line in postgresql.auto.conf, it fixed everything.

    0 讨论(0)
  • 2020-12-07 11:09

    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

    0 讨论(0)
  • 2020-12-07 11:09

    I could resolve this by setting the right permissions to datadir. It should be

    chmod 700 /var/lib/postgresql/10/main
    chown postgres.postgres /var/lib/postgresql/10/main
    
    0 讨论(0)
  • 2020-12-07 11:09

    If your service is not secure, this may be the reason

    vi /etc/postgresql/11/main/pg_hba.conf
    
    1. open hba config file, this config file usualy located in the etc directory.
    host    all   all    localhost trust   md5
    
    1. you can remove the trust keyword

    2. save pg_hba.conf

    3. sudo service postgresql restart.

    0 讨论(0)
提交回复
热议问题