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

问题描述:

我正在使用ubuntu 12.04rails 3.2。我正在创建一个rails应用程序,我在其中使用了PostgreSQL databse。Rails在应用程序中连接Postgresql数据库ubuntu

sudo apt-get install postgresql 

参考我检查出https://help.ubuntu.com/community/PostgreSQL:我使用下面的命令安装的PostgreSQL。后来我创建的用户的Postgres和使用下面的命令

sudo -u postgres psql postgres 
\password postgres 

接下来,我使用创建的数据库设置密码的Postgres:

sudo -u postgres createdb mydb 

我试图在PostgreSQL用户名为Postgres的和密码连接postgres并成功连接以下命令:

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} 

请帮我找出一个解决方案..

+0

做那些mydb_ * DB的存在吗? – dezso 2012-08-14 12:42:12

+0

是否将'host:localhost'添加到每个环境的底部可解决问题? – aguazales 2012-08-14 12:46:59

+0

这里有一个答案:http://*.com/questions/9987171/rails-3-2-fatal-peer-authentication-failed-for-user-pgerror – davidrac 2012-08-14 12:47:41

请您阳明添加主机为localhost

+0

我照你所说的做了,但我又犯了一个错误。我编辑了我的问题并提到了错误。 – 2012-08-14 12:50:32

+0

在您的主机中添加127.0.0.1,并删除yaml文件中的{} – 2012-08-14 12:57:45

+0

{}不存在于yaml文件中。我写了它来指定行号。 – 2012-08-14 13:11:45

我的建议:

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

我认为这些措施更有可能的工作,你想要什么。

我想你可能有2个问题。首先,没有被设置为Shreyas的主持人指出。但我相信第二个问题是,当你设置主机名时,Rails试图通过tcp套接字而不是本地ruby套接字连接到PostgreSQL。我的猜测是你需要修改你的pg_hba.conf文件以允许postgres通过本地主机登录。下面是几个可能有所帮助的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