Ruby-on-rails Postgres 无法连接到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13410686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:02:05  来源:igfitidea点击:

Postgres could not connect to server

ruby-on-railsdatabasepostgresqlmacoshomebrew

提问by Gary Lai

After I did brew update and brew upgrade, my postgres got some problem. I tried to uninstall postgres and install again, but it didn't work as well.

在我进行了 brew update 和 brew upgrade 之后,我的 postgres 遇到了一些问题。我尝试卸载 postgres 并重新安装,但效果不佳。

This is the error message.(I also got this error message when I try to do rake db:migrate)

这是错误消息。(当我尝试执行 rake db:migrate 时,我也收到了此错误消息)

$ psql
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

How can I solve it?

我该如何解决?

Mac version: Mountain lion.

Mac 版:山狮。

homebrew version: 0.9.3

自制版本:0.9.3

postgres version: psql (PostgreSQL) 9.2.1

postgres 版本:psql (PostgreSQL) 9.2.1

And this is what I did.

这就是我所做的。

12:30 ~/D/works$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.2.1...
12:31 ~/D/works$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/9.1.4...
12:31 ~/D/works$ psql --version
bash: /usr/local/bin/psql: No such file or directory
12:33 ~/D/works$ brew install postgresql
==> Downloading http://ftp.postgresql.org/pub/source/v9.2.1/postgresql-9.2.1.tar.bz2
Already downloaded: /Library/Caches/Homebrew/postgresql-9.2.1.tar.bz2
......
......
==> Summary
/usr/local/Cellar/postgresql/9.2.1: 2814 files, 38M, built in 2.7 minutes
12:37 ~/D/works$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "laigary".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
12:39 ~/D/works$ mkdir -p ~/Library/LaunchAgents
12:39 ~/D/works$   cp /usr/local/Cellar/postgresql/9.2.1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
12:39 ~/D/works$   launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
homebrew.mxcl.postgresql: Already loaded
12:39 ~/D/works$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
12:39 ~/D/works$ env ARCHFLAGS="-arch x86_64" gem install pg
Building native extensions.  This could take a while...
Successfully installed pg-0.14.1
1 gem installed
12:42 ~/D/works$ psql --version
psql (PostgreSQL) 9.2.1
12:42 ~/D/works$ psql
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Now, after I reinstalled howbrew,when I use $ psql, It doesn't show any error message.

现在,在我重新安装 howbrew 后,当我使用$ psql它时,它没有显示任何错误消息。

But I run rake db:migratein my rails app, it shows:

但是我rake db:migrate在我的 rails 应用程序中运行,它显示:

could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `connect'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:319:in `checkout_new_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `block (2 levels) in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `block in checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:in `block in connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:404:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `retrieve_connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:107:in `rescue in create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:51:in `create_database'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (3 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (2 levels) in <top (required)>'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `call'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `each'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/usr/local/Cellar/ruby/1.9.3-p327/lib/ruby/1.9.1/rake/application.rb:63:in `run'
/usr/local/bin/rake:32:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"riy_development", "pool"=>5, "username"=>nil, "password"=>nil}

Finally I find the solution.

最后我找到了解决方案。

$ sudo mkdir /var/pgsql_socket/
$ sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket/

This solution is little tricky, but it works. Hope anyone have a better solution

这个解决方案有点棘手,但它有效。希望有人有更好的解决方案

Update

更新

This works for me as well.

这也适用于我。

rm /usr/local/var/postgres/postmaster.pid

回答by jonathanpberger

Had a similar problem; a pid file was blocking postgres from starting up. To fix it:

有类似的问题;pid 文件阻止 postgres 启动。要解决这个问题:

rm /usr/local/var/postgres/postmaster.pid

rm /usr/local/var/postgres/postmaster.pid

and then all is well.

然后一切都很好。

回答by FreePender

This can sometimes be an issue with a postgres upgrade.

这有时可能是 postgres 升级的问题。

In my case, it happened when upgrading from 9.3 to 9.4.

就我而言,它发生在从 9.3 升级到 9.4 时。

See http://www.postgresql.org/docs/9.4/static/upgrading.html

请参阅http://www.postgresql.org/docs/9.4/static/upgrading.html

OS X/Homebrew:

OS X/自制软件:

Try running postgres -D /usr/local/var/postgres-- it will give you a much more verbose output if postgres fails to start.

尝试运行postgres -D /usr/local/var/postgres——如果 postgres 无法启动,它将为您提供更详细的输出。

In my case, running rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8removed my old databases and then reinitialized the postgres db schema.

就我而言,运行rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8删除了我的旧数据库,然后重新初始化了 postgres db 架构。

Thanks to https://github.com/Homebrew/homebrew/issues/35240for that solution.

感谢https://github.com/Homebrew/homebrew/issues/35240的解决方案。

After regenerating my databases (with rake db:create) everything worked fine again.

重新生成我的数据库(使用rake db:create)后,一切正常。

回答by Mosab Sasi

Found a solution that worked for me here:

在这里找到了一个对我有用的解决方案:

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

https://dba.stackexchange.com/questions/75214/psql-could-not-connect-to-server-no-such-file-or-directory

You basically run the following command to manually start the server:

您基本上运行以下命令来手动启动服务器:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

回答by svelandiag

If installing and uninstalling postgres with brew doesn't work for you, look at the logs of your postgresql installation or:

如果使用 brew 安装和卸载 postgres 对您不起作用,请查看您的 postgresql 安装日志或:

postgres -D /usr/local/var/postgres

if you see this kind of output:

如果你看到这种输出:

LOG:  skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 9.6.1.

Then try the following:

然后尝试以下操作:

rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

Then start the server:

然后启动服务器:

pg_ctl -D /usr/local/var/postgres -l logfile start

Source

来源

回答by Ryan Atallah

On Yosemite, if the pid file is blocking Postgres from starting and you have a launchctldaemon trying (and failing) to load the database daemons, then you'll need to unload the the plist file:

在 Yosemite 上,如果 pid 文件阻止 Postgres 启动,并且您有一个launchctl守护进程尝试(但失败)加载数据库守护进程,那么您需要卸载 plist 文件:

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Then remove the pid file

然后删除pid文件

$ rm /usr/local/var/postgres/postmaster.pid

Then reload the launchctldaemon

然后重新加载launchctl守护进程

$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

回答by David Winiecki

For anyone reading this and using Postgres.app, you may need host: localhostin your database.yml. http://postgresapp.com/documentation#toc_3

对于阅读本文并使用 Postgres.app 的任何人,您可能需要host: localhost在 database.yml 中。http://postgresapp.com/documentation#toc_3

回答by Tomer G

Upgrading the database works for me

升级数据库对我有用

brew postgresql-upgrade-database

brew postgresql-upgrade-database

回答by rld

brew services start postgres 

worked for me!

对我来说有效!

回答by jrwren

Check that the socket file exists.

检查套接字文件是否存在。

$ ls -l /tmp/.s.PGSQL.5432
srwxrwxrwx  1 you  wheel  0 Nov 16 09:22 /tmp/.s.PGSQL.5432

If it doesn't then check your postgresql.conf for unix_socket_directory change.

如果没有,请检查您的 postgresql.conf 是否有 unix_socket_directory 更改。

$ grep unix_socket /usr/local/var/postgres/postgresql.conf
#unix_socket_directory = ''     # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation

回答by rld

The problem can also be attributed to a crashed process that left postmaster.pid file behind. I do this and work's:

该问题也可以归因于一个崩溃的进程,导致 postmaster.pid 文件落后。我这样做并工作:

$ brew services stop postgresql
$ rm /usr/local/var/postgres/postmaster.pid # adjust path accordingly to your install
$ brew services start postgresql