如何为本地Rails项目设置Postgres数据库?

30

我最近购买了一台新机器,现在想从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服务器时,我得到以下内容:

enter image description here

3个回答

54

寻找同样的答案时,我看到了你的问题。我尝试按照@prasad.surase给出的指示进行操作。但我发现问题是ppa软件仓库将在12.04 LTS很快过时。相反,我找到了这个链接,它确实非常有帮助。

Ubuntu 12.04下用于Rails开发的PostgreSQL设置

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

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

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

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

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

  • create database projectname_development;
    create database projectname_test; 
    
  • 给用户在数据库上授予权限

  • 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’;
    

    18
    首先,安装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
    
    Gemfile
    gem 'pg'
    
    安装依赖包
    development.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';命令。 - Connor Leech
    2
    不需要用那种方式。使用rake db:create、rake db:migrate即可。您可以简单地使用rake db:setup来创建和迁移数据库。 - Prasad Surase
    执行 createuser -s -W user_name 命令时加上 -W 参数以强制要求输入密码。否则,它可能不会提示您输入密码,您将不得不使用 ALTER 语句在之后设置密码。 - Axel Tetzlaff

    2

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接