Rails Connect with Postgresql database in application ubuntu



我正在使用ubuntu 12.04rails 3.2。我正在创建一个使用PostgreSQL databse的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"

我尝试为每个环境将主机:本地主机添加到数据库.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}

请帮我找出解决方案。.

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

Rails 无法登录 postgresql - PG::错误 - 密码 - 正确信息

pg_hba.conf 中的"本地"和"本地主机"连接类型有什么区别?

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

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

我的建议:

步骤 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

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

最新更新