0

我正在使用ubuntu 12.04rails 3.2。我正在创建一个使用PostgreSQL数据库的 rails 应用程序。我使用以下命令安装了 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}

请帮我找出解决方案。

4

3 回答 3

1

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

Rails 无法登录到 postgresql - PG::Error - 密码 - 正确信息

pg_hba.conf 中的“local”和“localhost”连接类型有什么区别?

无法在 ubuntu 10.04 服务器上的 rails 3 的新数据库中使用 postgres 用户

于 2012-08-14T14:28:16.353 回答
0

请将主机作为 localhost 添加到您的 yml

于 2012-08-14T12:46:18.897 回答
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

我认为这些步骤比你正在尝试的更有可能奏效。

于 2012-08-14T13:57:41.150 回答