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

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


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

  • 我添加了一个数据库用户,该用户具有与我的Ubuntu帐户同名的数据库的超级用户权限。
  • 我用sudo service postgresql start数据库守护进程。
  • 我通过由Gerfried Fuchs维护的ppa:rhonda / pgadmin3安装了pgadmin3,版本1.14.0 Beta 1 。
  • 我可以使用我的用户帐户和密码以及端口5433通过pgadmin3连接。

我在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设置:
现在我想创建一个使用PostgreSQL的Rails应用程序。

  • 我通过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创建了一个Rails应用程序。
  • 我在config / database.yml中配置了user名和password ,用于开发测试,并删除了生产
  • 我在config / database.yml中配置了host: localhostport: 5433

这是我的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 

问题:
但是,当我运行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"} 

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

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

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

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

您可以在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