Postgresql适配器(pg):无法连接到服务器

spa*_*kle 65 postgresql ruby-on-rails pg rails-postgresql

每次运行我的Rails应用程序时都会收到此错误(它无法连接到我的本地Postgresql)

/Users/leonardo/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/
active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': 
could not connect to server: No such file or directory (PG::Error)
   Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

我正在使用Postgres.app,它正确运行.

如果我跑

$ psql
Run Code Online (Sandbox Code Playgroud)

我可以正确登录到Postgresql控制台.

$ which psql
 /Applications/Postgres.app/Contents/MacOS/bin/psql
Run Code Online (Sandbox Code Playgroud)

的Gemfile

source 'https://rubygems.org'
ruby "1.9.3"

gem 'rails', '3.2.11'
gem "pg"
Run Code Online (Sandbox Code Playgroud)

database.yml的

development:
  adapter: postgresql
  encoding: unicode
  username: leonardo
  password: 
  database: zapping
  port: 5432  
Run Code Online (Sandbox Code Playgroud)

Postgresql(控制台)

$ psql
leonardo=# \l
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

pja*_*jam 131

尝试添加host: localhost到您的database.yml.(基于:https://stackoverflow.com/a/10793186/919641)


Cra*_*ger 25

您的Pggem是针对预装在Mac OS X中的PostgreSQL libpq编译的,并且您使用的psql是在较新版本中安装的,反之亦然.

这可以通过添加localhost到指定TCP/IP连接来解决database.yml,但最好是Pg针对libpq您实际运行的服务器编译gem .为此,您应该能够在编译之前将PATH环境变量设置为正确的文件夹pg_config.在你的情况下,它将在某个地方Postgres.app.

  • 谢谢,这个答案帮我解决了.我采取的步骤很简单:1)`gem uninstall pg`,2)`bundle install`,完成. (16认同)

小智 24

如果添加后仍无效host: localhost,请将其删除postmaster.pid

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

  • 如果您的计算机崩溃并且您强制关闭但postgres没有正确终止,有时pid文件会卡在那里...这样做有效...删除它有助于让它再次运行. (5认同)

Bvu*_*Ic7 11

你应该添加host: localhost到你的数据库配置...

  • 我不知道为什么我会被投票,我在5分钟前回答!接受的答案(与我的帖子有相同的解决方案)? (2认同)

pan*_*kka 6

我在Mac上遇到了同样的问题.事实证明我在我的路径上的psql无法正常工作.尝试键入以下命令启动psql:

~/projects/some_project/project-rails$ psql
  psql: 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"?
Run Code Online (Sandbox Code Playgroud)

如果您看到这一点,则表示路径上的psql二进制文件正在尝试使用套接字进行连接而无法执行此操作(无论出于何种原因).由于我已经下载了pgadmin并且连接正常,我知道这不是服务器的问题.

通过向我的PATH添加正确版本的pgsql来修复它:

export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH
Run Code Online (Sandbox Code Playgroud)

现在psql(和rails)很高兴!

  • 还需要重新安装pg gem (5认同)

小智 6

我有同样的问题.你必须实际运行/启动postgres.Postgres最近必须停止在我的计算机上运行,​​所以我必须通过启动postgres服务器来确保它正在运行

postgres -D /usr/local/var/postgres
Run Code Online (Sandbox Code Playgroud)

然后以下命令(导致我有同样的错误)全部起作用:

bundle exec rake db:create db:migrate
bundle exec rspec
Run Code Online (Sandbox Code Playgroud)


Jar*_*ard 6

我有这个问题。这里的评论之一帮助我解决了这个问题。

谢谢,这个答案帮助我解决了它。我采取的步骤很简单:1)gem install pg,2)捆绑安装,完成。– haslo 2013年12月3日20:27

gem uninstall pg
bundle install
Run Code Online (Sandbox Code Playgroud)


Mr.*_*ene 6

由于这是我的搜索结果中出现的第一篇帖子,因此我决定发布更新后的修复程序.由于以上建议都不适合我.

brew postgresql-upgrade-database
Run Code Online (Sandbox Code Playgroud)

它升级了postgresql数据并移动了我的旧版本.我在9.6而不是10.4

这里找到解决方案


the*_*her 5

找到/ -name'postgresql.conf'

netstat -an | grep 5432 #=> /tmp/.s.PGSQL.5432

vi /用户/管理员/库/应用程序\支持/Postgres93/var/postgresql.conf

FROM: unix_socket_directories ='/ tmp'

到: unix_socket_directories ='/ var / pgsql_socket'

须藤mkdir / var / pgsql_socket

须藤chmod 777 / var / pgsql_socket