【问题标题】:Rails Connect with Postgresql database in application ubuntuRails 在应用程序 ubuntu 中与 Postgresql 数据库连接
【发布时间】:2012-08-14 12:39:21
【问题描述】:

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

请帮我找出解决方案..

【问题讨论】:

  • 在每个环境的底部添加host: localhost 是否可以解决问题?
  • 这里有一个答案:stackoverflow.com/questions/9987171/…
  • @dezso 你能告诉我一个命令来检查这些数据库是否存在。我检查了很多链接,但没有找到合适的解决方案。
  • 试试这个:psql -U postgres 然后\c mydb_production 例如。
  • 我认为 YAML 中有 2 的缩进?不?奇怪的是你应该得到 yaml 解析错误。

标签: ruby-on-rails database postgresql


【解决方案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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我按照你说的做了,但我又遇到了错误。我编辑了我的问题并提到了错误。
    • 在您的主机中添加 127.0.0.1,请删除您的 yaml 文件中的 {}
    • {} 不在 yaml 文件中。我只是写它来指定行号。
    • 那么请检查您的 YAML 文件中的空格。我猜 YAML 无法正确读取您的 yaml 文件。
    【解决方案3】:

    我的建议:

    第 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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多