我正在尝试创建一个使用 PostgreSQL 的 Rails 应用程序。这是我所做的描述。
PostgreSQL 设置:
我通过 ppa:pitti/postgresql 安装了 PostgreSQL 9.1.3由马丁皮特维护。之前安装了PostgreSQL 8.4;我不确定它是否仍然安装或消失。
- 我向与我的 Ubuntu 帐户同名的数据库添加了一个具有 super 用户权限的数据库用户。
- 我使用
sudo service postgresql start
启动数据库守护进程。 - 我安装了 pgadmin3, Version 1.14.0 Beta 1通过ppa:rhonda/pgadmin3由 Gerfried Fuchs 维护。
- 我可以使用我的用户帐户和密码以及端口 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 中为开发 和 配置了
用户
名称和密码
测试 并删除生产。 - 我在 config/database.yml 中配置了
host: localhost
和port: 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:最后,我看到test 环境的数据库配置遗漏了host 和port 的显式设置。将设置添加到 test 环境后,我能够成功运行命令 bundle exec rake db:create:all
。
我必须说,我不喜欢他们为 development 环境建议这些设置,但没有为其他环境添加它们。正如我证明的那样,这很可能会错过它们。
test:
adapter: postgresql
encoding: unicode
database: my_test_app_test
pool: 5
username: johndoe
password: password
host: localhost
port: 5433
关于ruby-on-rails - rails : rake db:create:all fails to connect to PostgreSQL database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263821/