Rails在应用程序ubuntu中与Postgresql数据库连接

时间:2012-08-14 12:39:21

标签: ruby-on-rails database postgresql

我正在使用 ubuntu 12.04 rails 3.2 。我正在创建一个rails应用程序,我正在使用 PostgreSQL 数据库。我使用以下命令安装了postgresql:

sudo apt-get install postgresql

作为参考,我签出了https://help.ubuntu.com/community/PostgreSQL。后来我创建了用户postgres并使用以下命令设置了密码postgres

sudo -u postgres psql postgres
\password postgres

接下来,我使用:

创建了数据库
 sudo -u postgres createdb mydb

我尝试使用用户名 postgres 和密码 postgres 连接Postgresql,并使用以下命令成功连接:

psql -U postgres -h localhost
Password for user postgres:
psql (9.1.4)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
postgres=#

在我的rails应用程序中,我的database.yml具有以下代码:

development:
 adapter: postgresql
 encoding: unicode
 database: mydb_development
 pool: 5
 username: postgres
 password: postgres

test:
 adapter: postgresql
 encoding: unicode
 database: mydb_test
 pool: 5
 username: postgres
 password: postgres

production:
 adapter: postgresql
 encoding: unicode
 database: mydb_production
 pool: 5
 username: postgres
 password: postgres

现在,当我运行命令rake db:migrate时,我收到以下错误:

rake aborted!
FATAL:  Peer authentication failed for user "postgres"

我尝试为每个环境添加host:localhost到database.yml,我收到以下错误:

rake aborted!
couldn't parse YAML at line 28 column 0

第28行是

development:
 adapter: postgresql
 encoding: unicode
 database: hackathonio_development
 pool: 5
 username: postgres
 password: testing
 host: localhost {This is line 28}

请帮我找出解决方案..

3 个答案:

答案 0 :(得分:1)

我想你可能有两个问题。首先,未被设置为Shreyas的主持人指出。但我相信第二个问题是,当你设置主机名时,Rails试图通过tcp套接字而不是本地ruby套接字连接到PostgreSQL。我的猜测是你需要修改你的pg_hba.conf文件以允许postgres通过localhost登录。以下是一些可能有帮助的答案的SO问题。

Rails can't login to postgresql - PG::Error - password - Correct info

What's the difference between "local" and "localhost" connection types in pg_hba.conf?

Can't use postgres user in new database for rails 3 on ubuntu 10.04 server

答案 1 :(得分:0)

请将您的yml主机添加为localhost

答案 2 :(得分:0)

我的建议:

第1步

通过运行

创建一个新的不同用户
$ createuser <username>
命令行上的

。系统会提示您输入密码等(例如/文档:http://www.postgresql.org/docs/8.1/static/app-createuser.html

第2步

使用新用户的用户名/密码更新database.yml。忘记你创建的第一个用户,至少现在。

第2步

$ rake db:create

第3步

$ rake db:migrate

我认为这些步骤更有可能与你正在尝试的工作有关。