PostgreSQL: Why psql can't connect to server?

前端 未结 22 2134
情书的邮戳
情书的邮戳 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条回答
  • 2020-12-07 11:10

    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. If not restart PostgreSQL service

    Step 2: Restart the pg_ctlcluster

    #format is pg_ctlcluster <version> <cluster> <action>
    sudo pg_ctlcluster 9.6 main start
    
    #restart PostgreSQL service
    sudo service postgresql restart
    

    Step 3: Step 2 failed and threw an error

    If restarting pg_lsclusters was not successful, it will throw an error. My error was(You can see the errors in the logs /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 for 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 postgresql restart
    
    0 讨论(0)
  • 2020-12-07 11:12

    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.

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

    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
    
    0 讨论(0)
  • 2020-12-07 11:15

    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.

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