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

时间:2012-04-21 23:04:49

标签: ruby-on-rails ruby-on-rails-3 postgresql postgresql-9.1 rails-postgresql

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


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

  • 我添加了一个数据库用户,该用户拥有与我的Ubuntu帐户同名的数据库的超级用户权限。
  • 我使用sudo service postgresql start启动数据库守护程序。
  • 我通过Gerfried Fuchs维护的pgadmin3, Version 1.14.0 Beta 1安装了ppa:rhonda/pgadmin3
  • 我可以使用我的用户帐户和密码以及端口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 中为开发 test 配置了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成功连接时使用的端口不同?

4 个答案:

答案 0 :(得分:18)

@Riateche:最后,我看到 test 环境的数据库配置错过了主机端口的显式设置。将设置添加到 test 环境后,我能够成功运行命令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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我遇到了同样的问题,可以通过运行此解决方案来解决

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
相关问题