繁体   English   中英

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

[英]Rails Connect with Postgresql database in application ubuntu

我正在使用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}

请帮我找出解决方案。

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

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

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

无法在新数据库中将Postgres用户用于ubuntu 10.04服务器上的Rails 3

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

我的建议:

第1步

通过运行来创建新的其他用户

$ createuser <username>

在命令行上。 系统将提示您输入密码等。(示例/文档: http : //www.postgresql.org/docs/8.1/static/app-createuser.html

第2步

使用新用户的用户名/密码更新database.yml 至少暂时不要忘记您创建的第一个用户。

第2步

$ rake db:create

第三步

$ rake db:migrate

我认为这些步骤比您尝试的更有可能起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM