【问题标题】:rails 3 postgreSQL basic 'database does not exist'rails 3 postgreSQL基本'数据库不存在'
【发布时间】:2011-12-13 15:48:59
【问题描述】:

好的,我正在构建我的第一个 rails 3.0 应用程序,并希望在我的开发机器(运行 10.6)上将 postgreSQL 服务器作为生产环境进行测试。当您创建一个新应用程序并 rake db:migrate 时,它​​会为所有三个环境创建 sqlite db。凉爽的。现在我想学习如何迁移到生产环境并使用 postgres。我已经使用自制软件安装了 postgres,安装了 pg (env ARCHFLAGS="-arch x86_64" gem install pg)postgres-pr gems

我已经运行了 rake db:migrate,希望它能够像使用 sqlite3 一样自动构建我的生产服务器,因为我已经更新了我的 database.yml(见下文)。

好的,在我的应用程序文件夹中,我使用“rails s --environment=production”重新启动服务器,但它说找不到我的生产数据库。

所以所有谷歌搜索“rails 3 postgres install”让我走到了这一步,但我似乎遗漏了一些东西,因为 rails 无法创建新的 pg 数据库。

postgres 正在运行,由 ps 确定。

createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production

但是这个目录没有这个数据库,所以我遗漏了一些东西。我忽略了什么?

这是我的 database.yml sn-p:

production:
  adapter: postgresql
  host: localhost
  database: db/vitae_production
  pool: 5
  timeout: 5000
  username: mysuperusername
  password:

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 postgresql


    【解决方案1】:

    这里发生了几件事。首先,您似乎在database.yml 中为database: 设置混合了SQLite 和PostgreSQL 格式。使用 SQLite,您可以 specify the relative path to the SQLite database file 使用以下内容:

    database: db/vitae_production.sqlite
    

    但是对于 PostgreSQL,你 specify the database name 是这样的:

    development:
      database: vitae_development
      username: rails
      ...
    

    然后,一旦设置了database.yml,您将在psql 内部create the database user(如果需要):

    psql> create role rails login;
    

    然后let Rails create the database:

    $ rake db:create
    

    然后您应该能够运行迁移以创建初始表并离开。

    您可能想阅读create role 文档以确保您获得正确的选项。您可能还想在开发环境中工作而不是在生产环境中工作,所以我更改了名称和 YAML 以反映这一点,生产是用于部署的,我认为您还没有部署任何东西。

    【讨论】:

      【解决方案2】:

      我不熟悉 Rails,但您可以将数据库创建为 postgres 超级用户,然后授予权限,假设在您的 linux 发行版中 postgres 超级用户是 postgres:

      su root
      su postgres
      createdb vitae_production
      

      然后在 postgres 中将权限授予另一个用户

      psql
      CREATE USER rails WITH PASSWORD 'myPassword';
      GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
      ALTER DATABASE vitae_production OWNER TO rails;
      

      那么你的配置文件应该是这样的:

      production:
        adapter: postgresql
        host: localhost
        database: vitae_production
        pool: 5
        timeout: 5000
        username: rails
        password: myPassword
      

      【讨论】:

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