【问题标题】:Rails: How do I change my database from SQLite to PG while in Development?Rails:如何在开发中将我的数据库从 SQLite 更改为 PG?
【发布时间】:2012-12-24 18:09:14
【问题描述】:

我使用默认的 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"

【问题讨论】:

  • 我认为你至少需要设置hostport参数;见stackoverflow.com/questions/10263821/…
  • 添加后我仍然收到角色不存在的错误。究竟什么是角色,如何创建新角色?
  • @Edmund - 对于简单版本,只需将角色视为用户。如果你真的想深入了解,the docs 有一个更长的讨论。

标签: ruby-on-rails postgresql


【解决方案1】:

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

sudo -u postgres psql

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

所以选项一是从您的​​ Rails 配置文件中删除密码选项,并希望一切正常。如果做不到这一点,通过编辑pg_hba.conf 文件(我的/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

希望有帮助!

【讨论】:

  • 嘿,泽维尔。当我键入该 sudo 命令时,我得到:sudo: unknown user: postgres。我记得以前玩过 pg_hba 文件,但我不记得如何找到它。你能告诉我如何在我的 mac 上找到那个文件吗/
  • @Edmund - 嗯。听起来您可能已经更改了默认用户名,那么?而且我不知道在 Mac 上哪里可以找到该文件,但它是一个非常独特的文件名 - 如果您搜索它应该会弹出。
【解决方案2】:

要将您的数据库转换为 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:

【讨论】:

  • 嘿,stsd,我想你在做点什么!但是,当我尝试时出现身份验证错误。我用底部的错误更新了我的答案。请看一下,告诉我有什么问题!
  • @Edmund,试试这个:sudo -u postgres createuser -s foo
  • 您使用的是什么操作系统?如果您在 Debian/ubuntu 上,那么您应该能够使用以下命令登录到 postgresql:sudo -u postgres psql -U postgres 然后您可以发出任何命令,例如; createuser
  • 如果上面的评论不行,那么你可以试试:sudo -u postgres psql
  • 同样的错误... Edmunds-MacBook-Pro:sample_app edmundmai$ sudo -u postgres psql sudo:未知用户:postgres
【解决方案3】:
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`

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2016-03-05
    • 2018-10-23
    • 2019-12-27
    • 2016-05-02
    • 2013-12-02
    • 2019-05-29
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多