2

我使用默认的 SQLite 数据库生成了我的 rails 应用程序,但是在创建了几个模型并迁移了几次之后,我想将其更改为 Postgresql。

我将 postgres gem 添加到我的 Gemfile 中,捆绑安装,然后我替换了所有 database.yml 代码

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password: mypass

development:
  <<: *default
  database: sample_app_development

test:
  <<: *default
  database: sample_app_test

production:
  <<: *default
  database: sample_app_production

FATAL: password authentication failed for user "postgres"即使密码正确,我也会收到错误消息。是因为我错过了一步吗?我应该使用 pg Admin III 告诉 PG 我想将此应用程序添加到我的服务器吗?我应该创建一个新角色/连接吗?

我遇到过几次这个问题,似乎无法找到这个特定问题的答案。

当我尝试运行时它给了我这个rake db:drop

Couldn't drop sample_app_development : #<PGError: FATAL:  role "postgres" does not exist
>
Couldn't drop sample_app_test : #<PGError: FATAL:  role "postgres" does not exist
>

=========

Edmunds-MacBook-Pro:sample_app edmundmai$ createuser foo
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
Password: 
createuser: could not connect to database postgres: FATAL:  password authentication failed for user "edmundmai"
4

3 回答 3

2

Postgres 用户认证有点奇怪。默认是使用与操作系统相同的身份验证(至少在 Linux 中)。因此,要从命令行进入 Postgres 提示符,您必须执行以下操作:

sudo -u postgres psql

请注意,没有密码 - 由于操作系统负责身份验证,因此不需要密码(不过,如果需要,操作系统会询问您的 sudo 密码)。

因此,选项一是从 Rails 配置文件中删除密码选项,并希望一切正常。pg_hba.conf如果做不到这一点,请通过编辑文件(我的 at /etc/postgresql/9.2/main/pg_hba.conf)设置 Postgres 以接受基于密码的身份验证。这是我本地服务器的示例;用户“postgres”使用操作系统的身份验证(“peer”),但用户“opengeo”使用密码(“md5”):

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             postgres                                peer
local   all             opengeo                                 md5

希望有帮助!

于 2012-12-24T18:40:47.233 回答
1

要将您的数据库转换为 postgresql,首先创建一个用户,如下所示:

$ createuser foo
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

创建数据库:

CREATE DATABASE foo_db ENCODING 'UTF8' OWNER foo;

确保您的 database.yml 如下所示:

development:
  adapter: postgresql
  encoding: unicode
  database: foo_db
  pool: 5
  username: foo
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: foo_test
  pool: 5
  username: foo
  password:
于 2012-12-24T18:46:17.373 回答
0
development:
  adapter: postgresql
  database: postgres
  username: postgres
  password: ;ernakulam
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  database: postgres
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: postgres
  pool: 5
  timeout: 5000`
于 2013-10-13T10:13:32.877 回答