Rails 将默认项目数据库'sqllite'更改为 PostgreSql



我正在学习Ruby。我创建了一些应用程序并使用它。

我想将数据库更改为Postgres。我用

修改了gemfile
gem 'pg'

[PG已经安装在USR/bin/pg中]然后使用使用SQLLITE的postgresql。

PG连接不好:

FATAL: role "user" does not exist 

我已经创建了一些Postgres用户。与Postgres一起工作,我应该更改什么?

development:
  adapter: postgresql
  database: db/development.postgresql
  pool: 5
  timeout: 5000
user:
  adapter: postgresql
  database: db/test.postgresql
  pool: 5
  timeout: 5000
production:
  adapter: postgresql
  database: db/production.postgresql
  pool: 5
  timeout: 5000

如果未指定用户名连接到PostgreSQL,则libpq将使用当前运行用户的登录用户名进行连接。

libpq是Rails使用的Pg GEM用于连接到PostgreSQL的用途。

因此,在您的database.yml中,指定PostgreSQL用户与user:条目一样连接到数据库。确保这对应于PostgreSQL中的用户,并且pg_hba.conf允许该用户连接。

要了解更多信息,请参阅PostgreSQL文档的客户端认证章节。

我知道这是一个两年的问题,但是我遇到了相同的问题,然后我通过更改config.yml的语法修复了它,然后我rake db:迁移

default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: yourappnamehere_development

test:
<<: *default
database: yourappnamehere_testdevelopment:

production:
<<: *default
database: YOURAPPNAME_production
username: YOURUSERNAME
password: <%= ENV['YOURAPPNAME_DATABASE_PASSWORD'] %>

最新更新