Rails:rake db:create:all无法连接到PostgreSQL数据库 [英] Rails: rake db:create:all fails to connect to PostgreSQL database

查看:105
本文介绍了Rails:rake db:create:all无法连接到PostgreSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a Rails app that uses PostgreSQL. Here is a description of what I did.

PostgreSQL设置:

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

PostgreSQL setup:
I installed PostgreSQL 9.1.3 via the ppa:pitti/postgresql maintained by Martin Pitt. There was PostgreSQL 8.4 installed before; I am not sure if it is still installed or gone.


  • 我向具有相同名称的数据库添加了具有超级用户权限的数据库用户作为我的Ubuntu帐户。

  • 我使用 sudo服务postgresql start 启动数据库守护程序。

  • 我通过 pgadmin3,版本1.14.0 Beta 1 。 / + archive / pgadmin3 rel = noreferrer> ppa:rhonda / pgadmin3 由Gerfried Fuchs维护。

  • 我可以使用用户帐户,密码和端口通过pgadmin3连接5433。

  • I added a database user with superuser rights to the database that has the same name as my Ubuntu account.
  • I start the database daemon with sudo service postgresql start.
  • I installed pgadmin3, Version 1.14.0 Beta 1 via ppa:rhonda/pgadmin3 maintained by Gerfried Fuchs.
  • I can connect via pgadmin3 using my user account and password and port 5433.

我在 pg_hba.conf 中的postgres配置如下(为便于阅读,删除了注释)。 / p>

My postgres configuration in pg_hba.conf is as follows (removed comments for readability).

[...]
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应用程序。


Rails setup:
Now I want to create a Rails application that uses PostgreSQL.


  • I通过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应用程序。 / li>
  • 我在 config /数据库中配置了用户名称和密码 .yml 用于开发 test ,并删除了生产

  • 我配置了<$在 config / database.yml 中的c $ c> host:localhost 和 port:5433

  • I installed Ruby 1.9.3-p125 via RVM.
  • I installed Rails 3.2.3 into the Gemset ruby-1.9.3-p125@global.
  • I created a .rvmrc and Gemset for the application.
  • I created a Rails application via rails new my_test_app -d postgresql.
  • I configured the user name and password in config/database.yml for development and test and removed production.
  • I configured host: localhost and port: 5433 in config/database.yml.

这是我的 config / database.yml 的内容(为便于阅读,删除了注释)。

Here is the content of my config/database.yml (removed comments for readability).

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 时,会收到以下错误消息。


Problem:
However, when I run bundle exec rake db:create:all I receive the following error message.

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成功连接时使用了吗?


Question:
Why is the port different to the one I use when I successfully connect via pgadmin3?

推荐答案

@Riateche:最后,我看到 test的数据库配置环境缺少主机端口的显式设置。将设置添加到 test 环境后,我能够成功运行命令 bundle exec rake db:create:all

我必须说,我不喜欢他们为 development 环境建议这些设置,但没有为其他环境添加这些设置。正如我证明的那样,这很可能会错过它们。

@Riateche: Finally, I saw that the database configuration for test environment misses the explicit settings for host and port. After I added the settings to the test environment, I was able to run the command bundle exec rake db:create:all successfully.
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

这篇关于Rails:rake db:create:all无法连接到PostgreSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆