【问题标题】:How to set up Postgres database for local Rails project?如何为本地 Rails 项目设置 Postgres 数据库?
【发布时间】:2013-11-26 01:07:18
【问题描述】:

我最近买了一台新机器,现在想在 Github 上处理我的项目。我很好奇如何在我的本地机器上正确设置 Postgres 数据库。我在 Ubuntu (12.04) 上安装了 postgresqlpgadmin3libpq-dev

我拉下项目:

git clone https://github.com/thebenedict/cowsnhills.git

然后运行:

bundle.

当我跑步时:

rake db:create && rake db:schema:load

我收到此错误:

rake db:create && rake db:schema:load
FATAL:  password authentication failed for user "cnh"
FATAL:  password authentication failed for user "cnh"
....

config/database.yml 文件如下所示:

development:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: cnh_development
  pool: 5
  username: cnh
  password: cnh

test:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: cnh_test
  pool: 5
  username: cnh
  password: cnh

production:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: cnh_production
  pool: 5
  username: cnh
  password: cnh

设置 Postgres 数据库以便我可以在本地计算机上运行此项目的正确方法是什么?

现在当我启动 Rails 服务器时,我得到:

【问题讨论】:

    标签: ruby-on-rails postgresql ubuntu postgresql-9.1 rails-postgresql


    【解决方案1】:

    我在寻找相同答案时遇到了您的问题。我试图按照@prasad.surase 给你的指示去做。我发现的问题是 ppa 存储库将很快在 12.04 LTS 上贬值。相反,我找到了这个链接,它真的很有帮助。

    PostgreSQL setup for Rails development in Ubuntu 12.04

    1. 通过包管理器安装 postgresql 和管理工具

      sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3
      
    2. 以 postgres 用户身份登录到 postgresql 提示符

      sudo su postgres -c psql 
      
    3. 为您的项目创建一个 postgresql 用户

      create user username with password 'password';
      
    4. 使用与 Ubuntu 用户相同的名称和密码设置您的 postgres 用户,并使他成为 postgres 超级用户

      alter user username superuser; 
      
    5. 创建开发和测试数据库

      create database projectname_development;
      create database projectname_test; 
      
    6. 向用户授予数据库权限

      grant all privileges on database projectname_development to username;
      grant all privileges on database projectname_test to username; 
      

    结束 postgresql 会话类型\q

    更新用户密码

    alter user username with password ‘new password’;
    

    【讨论】:

      【解决方案2】:
      首先,安装postgresql
      sudo add-apt-repository ppa:pitti/postgresql
      sudo apt-get update
      
      #now install postgresql
      sudo apt-get install postgresql-9.1 libpq-dev
      
      在 psql 中创建一个新用户
      sudo su postgres
      createuser user_name #Shall the new role be a superuser? (y/n) y
      
      宝石文件
      gem 'pg'
      

      捆绑安装

      开发.yml
      development:
        adapter: postgresql
        database: app_development
        pool: 5
        username: user_name
        password:
      

      【讨论】:

      • 我还必须create database app_development owner user_name; 然后alter user_name password 'password';
      • 不需要那样做。使用 rake db:create, rake db:migrate 。你可以简单地做 rake db:setup 来创建和迁移。
      • 使用-W 执行createuser 以强制提示密码createuser -s -W user_name。否则它可能不会要求您提供一个,您必须在之后使用ALTER-Statement 进行设置
      【解决方案3】:

      您点击此链接http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/

      创建 postgres 用户并替换 database.yml 中的凭据

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-12
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        相关资源
        最近更新 更多