[英]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.我不确定它是否仍然安装或消失了。
sudo service postgresql start
.我用sudo service postgresql start
数据库守护进程。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 应用程序。
rails new my_test_app -d postgresql
.我通过rails new my_test_app -d postgresql
创建了一个 Rails 应用程序。user
name and password
in config/database.yml for development and test and removed production .我在config/database.yml 中配置了user
名和password
用于开发和测试并删除了production 。host: localhost
and port: 5433
in config/database.yml .我在config/database.yml 中配置了host: localhost
和port: 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:最后,我看到测试环境的数据库配置遗漏了host和port的显式设置。 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 (例如psql或pgAdmin ),则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.