Ruby-on-rails Rails:rake db:create:all 无法连接到 PostgreSQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10263821/
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-03 03:16:43  来源:igfitidea点击:

Rails: rake db:create:all fails to connect to PostgreSQL database

ruby-on-railsruby-on-rails-3postgresqlpostgresql-9.1rails-postgresql

提问by JJD

I am trying to create a Rails app that uses PostgreSQL. Here is a description of what I did.

我正在尝试创建一个使用 PostgreSQL 的 Rails 应用程序。这是我所做的描述。



PostgreSQL setup:
I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresqlmaintained by Martin Pitt. There was PostgreSQL 8.4 installed before; I am not sure if it is still installed or gone.

PostgreSQL 设置:
我通过 Martin Pitt 维护的ppa:pitti/postgresql安装了 PostgreSQL 9.1.3 。之前安装了PostgreSQL 8.4;我不确定它是否仍然安装或消失了。

  • I added a database user with superuser rights to the database that has the same name as my Ubuntu account.
  • I start the database daemon with sudo service postgresql start.
  • I installed pgadmin3, Version 1.14.0 Beta 1via ppa:rhonda/pgadmin3maintained by Gerfried Fuchs.
  • I can connect via pgadmin3 using my user account and password and port 5433.
  • 我向与我的 Ubuntu 帐户同名的数据库添加了一个具有超级用户权限的数据库用户。
  • 我用sudo service postgresql start.
  • 我通过由 Gerfried Fuchs 维护的ppa:rhonda/pgadmin3安装了pgadmin3,版本 1.14.0 Beta 1
  • 我可以使用我的用户帐户和密码以及端口 5433 通过 pgadmin3 进行连接。

My postgres configuration in pg_hba.confis as follows (removed comments for readability).

我在pg_hba.conf 中的postgres 配置如下(为了可读性删除了注释)。

[...]
local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5


Rails setup:
Now I want to create a Rails application that uses PostgreSQL.

Rails 设置:
现在我想创建一个使用 PostgreSQL 的 Rails 应用程序。

  • I installed Ruby 1.9.3-p125 via RVM.
  • I installed Rails 3.2.3 into the Gemset ruby-1.9.3-p125@global.
  • I created a .rvmrc and Gemset for the application.
  • I created a Rails application via rails new my_test_app -d postgresql.
  • I configured the username and passwordin config/database.ymlfor developmentand testand removed production.
  • I configured host: localhostand port: 5433in config/database.yml.
  • 我通过 RVM 安装了 Ruby 1.9.3-p125。
  • 我将 Rails 3.2.3 安装到 Gemset ruby​​-1.9.3-p125@global 中。
  • 我为应用程序创建了一个 .rvmrc 和 Gemset。
  • 我通过rails new my_test_app -d postgresql.
  • 我在config/database.yml 中配置了user名称和用于开发测试并删除了productionpassword
  • 余配置host: localhostport: 5433配置/ database.yml中

Here is the content of my config/database.yml(removed comments for readability).

这是我的config/database.yml的内容(为了可读性删除了注释)。

development:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_development
  pool: 5
  username: johndoe
  password: password    
  host: localhost
  port: 5433

test:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_test
  pool: 5
  username: johndoe
  password: password


Problem:
However, when I run bundle exec rake db:create:allI receive the following error message.

问题:
但是,当我运行时,bundle exec rake db:create:all我收到以下错误消息。

could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket
"/var/run/postgresql/.s.PGSQL.5432"?
[...]
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode",
"database"=>"my_test_app_test", "pool"=>5, "username"=>"johndoe",
"password"=>"password"}


Question:
Why is the port different to the one I use when I successfully connect via pgadmin3?

问题:
为什么我通过 pgadmin3 成功连接时使用的端口与我使用的端口不同?

回答by JJD

@Riateche: Finally, I saw that the database configuration for testenvironment misses the explicit settings for hostand port. After I added the settings to the testenvironment, I was able to run the command bundle exec rake db:create:allsuccessfully.
I must say, I do not like that they suggest those settings for the developmentenviroment, but did not add them for the other environments. That makes it very likely to miss them, as I proofed.

@Riateche:最后,我看到测试环境的数据库配置遗漏了hostport的显式设置。将设置添加到测试环境后,我能够bundle exec rake db:create:all成功运行命令。
我必须说,我不喜欢他们为开发环境建议这些设置,但没有为其他环境添加它们。正如我所证明的那样,这使得很可能会错过它们。

test:
  adapter: postgresql
  encoding: unicode
  database: my_test_app_test
  pool: 5
  username: johndoe
  password: password
  host: localhost
  port: 5433

回答by Anish

If any psqlclient session is accessing template1( for example psqlor pgAdmin), rake db:migratefails. Close any sessions before doing rake db:migrate.

如果任何psql客户端会话正在访问template1(例如psqlpgAdmin), 则会 rake db:migrate失败。在做之前关闭所有会话 rake db:migrate

回答by Hetdev

you can change your postgresql configuration in pg_hba.conf to trust.

您可以将 pg_hba.conf 中的 postgresql 配置更改为信任。

[...]
local   all             postgres                                peer
local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5