简体   繁体   English

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

[英]Postgresql adapter (pg): could not connect to server

I get this error every this I run my Rails app (It cannot connect to my local 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"?

I'm using Postgres.app that it's correctly running.我正在使用正确运行的Postgres.app

If I run如果我跑

$ psql

I can login properly to Postgresql console.我可以正确登录到 Postgresql 控制台。

$ which psql
 /Applications/Postgres.app/Contents/MacOS/bin/psql

Gemfile文件

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

gem 'rails', '3.2.11'
gem "pg"

database.yml数据库.yml

development:
  adapter: postgresql
  encoding: unicode
  username: leonardo
  password: 
  database: zapping
  port: 5432  

Postgresql (Console) Postgresql (控制台)

$ psql
leonardo=# \l

在此处输入图片说明

Try adding host: localhost to your database.yml.尝试将host: localhost添加到您的 database.yml。 (Based on: https://stackoverflow.com/a/10793186/919641 ) (基于: https : //stackoverflow.com/a/10793186/919641

Your Pg gem was compiled against the PostgreSQL libpq pre-installed in Mac OS X and you're using the psql that you installed in a newer version, or vice versa.您的Pg gem 是针对 Mac OS X 中预安装的 PostgreSQL libpq 编译的,并且您使用的是在较新版本中安装的psql ,反之亦然。

This can be worked around by specifying a TCP/IP connection, by adding localhost to database.yml , but it's better to compile the Pg gem against the libpq for the server you're actually running.这可以通过指定 TCP/IP 连接,通过将localhost添加到database.yml ,但最好针对您实际运行的服务器的libpq编译Pg gem。 To do that, you should be able to set the PATH environment variable to the folder with the correct pg_config in it before compiling.为此,您应该能够在编译之前将PATH环境变量设置为包含正确pg_config的文件夹。 In your case that'll be somewhere within Postgres.app .在您的情况下,它将位于Postgres.app某个位置。

如果在添加host: localhost后它仍然不起作用,请删除postmaster.pid

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

您应该将host: localhost添加到您的数据库配置中...

I had the same problem on a Mac.我在 Mac 上遇到了同样的问题。 Turns out that the psql I had on my path wasn't working correctly.原来我的路径上的 psql 无法正常工作。 Try launching psql by typing:尝试通过键入以下内容启动 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"?

If you see this, it indicates the psql binary on your path is attempting to connect using socket and unable to do so (for whatever reason).如果您看到这一点,则表示您路径上的 psql 二进制文件正在尝试使用套接字进行连接,但无法连接(无论出于何种原因)。 Since I had already downloaded pgadmin and it was connecting fine, I knew it wasn't an issue with the server.由于我已经下载了pgadmin并且连接正常,我知道这不是服务器的问题。

Fixed it by adding the right version of pgsql to my PATH:通过将正确版本的 pgsql 添加到我的 PATH 来修复它

export PATH=/Applications/Postgres.app/Contents/MacOS/bin:$PATH

Now psql (and rails) are happy!现在 psql(和 rails)很高兴!

I had this same issue.我有同样的问题。 You have to actually run / start postgres.您必须实际运行/启动 postgres。 Postgres must have stopped running on my computer recently, so I had to make sure it's running by starting the postgres server Postgres 最近一定在我的电脑上停止运行了,所以我必须通过启动 postgres 服务器来确保它正在运行

postgres -D /usr/local/var/postgres

Then the following commands (which were causing me the same error you had) all worked:然后以下命令(这导致我遇到了与您相同的错误)都有效:

bundle exec rake db:create db:migrate
bundle exec rspec

I had this issue.我有这个问题。 One of the comments here helped me fix the issue.这里的评论之一帮助我解决了这个问题。

Thanks, this answer helped me fix it.谢谢,这个答案帮助我解决了这个问题。 The steps I took were simple: 1) gem uninstall pg, 2) bundle install, done.我采取的步骤很简单:1)gem uninstall pg,2)bundle install,完成。 – haslo Dec 3 '13 at 20:27 – 哈斯洛 2013 年 12 月 3 日 20:27

gem uninstall pg
bundle install

Since This was the first post that appeared in my search result, I have decided to post the updated fix for this.由于这是我的搜索结果中出现的第一篇文章,因此我决定为此发布更新的修复程序。 Since none of the above suggestion worked for me.由于上述建议都不适合我。

brew postgresql-upgrade-database

It upgraded postgresql data and moved my old version.它升级了 postgresql 数据并移动了我的旧版本。 I was on 9.6 instead of 10.4我在 9.6 而不是 10.4

Found the solution here这里找到了解决方案

find / -name 'postgresql.conf' find / -name 'postgresql.conf'

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

vi /Users/admin/Library/Application\\ Support/Postgres93/var/postgresql.conf vi /Users/admin/Library/Application\\ Support/Postgres93/var/postgresql.conf

FROM: unix_socket_directories = '/tmp'来自: unix_socket_directories = '/tmp'

TO: unix_socket_directories = '/var/pgsql_socket'至: unix_socket_directories = '/var/pgsql_socket'

sudo mkdir /var/pgsql_socket须藤 mkdir /var/pgsql_socket

sudo chmod 777 /var/pgsql_socket须藤 chmod 777 /var/pgsql_socket

For heroku this is all you need.对于heroku,这就是你所需要的。

heroku addons:create heroku-postgresql

production:
 adapter: postgresql
 encoding: unicode
 host: localhost
 # For details on connection pooling, see rails configuration guide
 # http://guides.rubyonrails.org/configuring.html#database-pooling
 pool: 5

我只是遇到了 postgres 应用程序没有在我的 Mac 上运行的问题...

On Mac, I had different versions of postgresql.在 Mac 上,我有不同版本的 postgresql。 This problem has been solved when I tried brew switch postgresql 9.5.5 (I don't think version number matters at this point)当我尝试brew switch postgresql 9.5.5时,这个问题已经解决了(我认为此时版本号并不重要)

$ brew switch postgresql 9.5.5
Cleaning /usr/local/Cellar/postgresql/9.6.1
Cleaning /usr/local/Cellar/postgresql/9.6.5
Cleaning /usr/local/Cellar/postgresql/10.2
Cleaning /usr/local/Cellar/postgresql/10.3
Cleaning /usr/local/Cellar/postgresql/9.5.5
Cleaning /usr/local/Cellar/postgresql/9.5.4

All data will be gone away, by the way.顺便说一下,所有数据都将消失。 I guess there's a brew command for doing this without switching version, which I couldn't find.我想有一个 brew 命令可以在不切换版本的情况下执行此操作,我找不到。

I had the same problem on OS High sierra 10.13 I followed the instructions from this website I downloaded version 10.4 ref: https://postgresapp.com/我在 OS High sierra 10.13 上遇到了同样的问题我按照本网站的说明下载了 10.4 版参考: https : //postgresapp.com/

Then I added this to bash profile: export PATH=$PATH:/Library/PostgreSQL/10.4/bin:$PATH然后我将其添加到 bash 配置文件中: export PATH=$PATH:/Library/PostgreSQL/10.4/bin:$PATH

re start terminal.重新启动终端。

Open new window terminal and then type: psql -U postgres打开新的窗口终端,然后输入: psql -U postgres

after that you will see this message: psql (10.4) Type "help" for help.之后,您将看到此消息: psql (10.4) 键入“help”以获得帮助。

postgres=# postgres=#

我的问题是/etc/hosts文件已更改并且没有此关键条目: 127.0.0.1 localhost

I had a similar issue - when I ran psql command in terminal I still got the same error, so that told me that it wasn't a problem with the pg gem and I should check the postgres setup itself.我有一个类似的问题 - 当我在终端中运行psql命令时,我仍然遇到同样的错误,所以这告诉我 pg gem 没有问题,我应该检查 postgres 设置本身。 So I checked the postgres logs - in my case they were located at /usr/local/var/log/postgres.log and I saw the following error:所以我检查了 postgres 日志 - 在我的情况下它们位于/usr/local/var/log/postgres.log并且我看到以下错误:

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.66.dylib
  Referenced from: /usr/local/opt/postgresql/bin/postgres

So knowing the exact error helped me resolve the issue, in my case based on this answer I ran所以知道确切的错误帮助我解决了这个问题,在我的情况下,我运行了这个答案

brew switch icu4c 66.1

which resolved the issue after restarting postgres:重新启动postgres后解决了这个问题:

brew services restart postgresql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在 Manjaro 上安装 postgresql 时,PG 无法连接到服务器 - PG could not connect to server when trying to install postgresql on Manjaro PG :: ConnectionBad,无法连接到服务器 - PG::ConnectionBad, could not connect to server PG ConnectionBad 无法连接到服务器 - PG ConnectionBad Could not connect to server Rails db:create using PostgreSQL return PG::ConnectionBad: 无法连接到服务器:连接被拒绝 - Rails db:create using PostgreSQL return PG::ConnectionBad: could not connect to server: Connection refused Appfog + Rails 3 + Postgresql + IronWorker =>无法连接到服务器:连接超时(PG :: Error) - Appfog + Rails 3 + Postgresql + IronWorker => could not connect to server: Connection timed out (PG::Error) PG ::错误:无法连接到服务器:连接被拒绝 - PG::Error: could not connect to server: Connection refused PG::ConnectionBad - 无法连接到服务器:连接被拒绝 - PG::ConnectionBad - could not connect to server: Connection refused PG :: ConnectionBad:无法连接到服务器(heroku) - PG::ConnectionBad: could not connect to server (heroku) PG :: ConnectionBad:无法连接到服务器:连接被拒绝 - PG::ConnectionBad: could not connect to server: Connection refused 无法连接到服务器:没有这样的文件或目录(PG::ConnectionBad) - could not connect to server: No such file or directory (PG::ConnectionBad)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM