PostgreSQL error 'Could not connect to server: No such file or directory'

前端 未结 22 1818
梦谈多话
梦谈多话 2020-11-29 17:20

Like some others I am getting this error when I run rake db:migrate in my project or even try most database tasks for my Ruby on Rails 3.2 applications.

相关标签:
22条回答
  • 2020-11-29 17:54

    If you're on MacOS and using homebrew I found this answer extremely helpful:

    https://stackoverflow.com/a/27708774/4062901

    For me, my server was running but because of an upgrade I was having issues connecting (note: I use brew services). If Postgres is running, try cat /usr/local/var/postgres/server.log and see what the logs say. My error was a migration, not a connection issue.

    Also after the manual migration they propose (which does work but) I found there was no longer a table for my user. Try this command to fix that: createdb (answered via psql: FATAL: database "<user>" does not exist)

    0 讨论(0)
  • 2020-11-29 17:57

    One reason you get this error is that your local postgres database shuts down when you restart your computer. In a new terminal window, simply type:

    $psql -h localhost 
    

    to restart the server.

    0 讨论(0)
  • 2020-11-29 17:57

    Just restart Postgres as soon as error appears by running brew services restart postgresql and then try again

    0 讨论(0)
  • 2020-11-29 17:57

    There might be different issues with running PostgreSQL locally. I would recommend to clean all versions of postgres installed and start fresh. Once you have it installed, it's very easy to recreate your database if you have your rails project with the up to date db/schema.rb

    Here is how I usually install PostgreSQL on a Mac. If you're running your database under the root user locally, you might want to omit the last step that creates a todo user

    0 讨论(0)
  • 2020-11-29 18:01

    For me, this works

    rm /usr/local/var/postgres/postmaster.pid
    
    0 讨论(0)
  • 2020-11-29 18:01

    When you run: psql -p 5432 -h localhost it does not use the loopback driver but uses a real socket and hence you need to configure postgres to accept socket connections. Hence even though pgadmin and other clients may work, psql will not.

    You need to change both the postgresql.conf file and the pg_hba.conf files so allow connection over a real socket. This will also allow connection from a remote ip which is why it is disabled by default.

    These files are in the data directory but the actual location can be different depending on how postgres was installed. With postgres running, run the command:

    ps -ef | grep postmaster
    

    These two files are the -D directory (maybe /usr/local/pgsql/data).

    For the postgresql.conf file, uncomment the listen_address and change it to be:

    listen_address = '*'
    

    For the pg_hba.conf add the line:

    host all all 0.0.0.0/0 md5
    
    0 讨论(0)
提交回复
热议问题