简体   繁体   English

Rails:rake db:create:all 无法连接到 PostgreSQL 数据库

[英]Rails: rake db:create:all fails to connect to PostgreSQL database

I am trying to create a Rails app that uses PostgreSQL.我正在尝试创建一个使用 PostgreSQL 的 Rails 应用程序。 Here is a description of what I did.这是我所做的描述。


PostgreSQL setup: PostgreSQL 设置:
I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresql maintained by Martin Pitt.我通过 Martin Pitt 维护的ppa:pitti/postgresql安装了 PostgreSQL 9.1.3。 There was PostgreSQL 8.4 installed before;之前安装了PostgreSQL 8.4; I am not sure if it is still installed or gone.我不确定它是否仍然安装或消失了。

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

My postgres configuration in pg_hba.conf is 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.现在我想创建一个使用 PostgreSQL 的 Rails 应用程序。

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

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:all I 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 成功连接时使用的端口不同?

@Riateche: Finally, I saw that the database configuration for test environment misses the explicit settings for host and port . @Riateche:最后,我看到测试环境的数据库配置遗漏了hostport的显式设置。 After I added the settings to the test environment, I was able to run the command bundle exec rake db:create:all successfully.将设置添加到测试环境后,我能够成功运行命令bundle exec rake db:create:all
I must say, I do not like that they suggest those settings for the development enviroment, but did not add them for the other environments.我必须说,我不喜欢他们为开发环境建议这些设置,但没有为其他环境添加它们。 That makes it very likely to miss them, as I proofed.正如我所证明的那样,这使得很可能会错过它们。

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

If any psql client session is accessing template1 ( for example psql or pgAdmin ), rake db:migrate fails.如果任何psql客户端会话正在访问template1 (例如psqlpgAdmin ),则rake db:migrate失败。 Close any sessions before doing rake db:migrate .在执行rake db:migrate之前关闭所有会话。

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

我遇到了同样的问题,我通过运行这个解决方案来解决它

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM