30

I recently got a new machine and would now like to work on my projects from Github. I'm curious as to how to properly set up the Postgres database on my local machine. I have postgresql, pgadmin3 and libpq-dev installed on Ubuntu (12.04).

I pull down the project:

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

and run:

bundle.

When I run:

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

I get this error:

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

The config/database.yml file looks like this:

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

What's the proper way to set up the Postgres database so I can run this project on my local machine?

Right now when I start the Rails server I get:

enter image description here

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
Connor Leech
  • 18,052
  • 30
  • 105
  • 150

3 Answers3

54

I came across your question when looking for the same answer. I attempted to follow the instructions @prasad.surase gave you. The problem I found is the ppa repository is going to depreciate soon on 12.04 LTS. Instead I found this link and it really helped.

PostgreSQL setup for Rails development in Ubuntu 12.04

  1. Install postgresql and admin tools through the package manager

    sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3
    
  2. Login to postgresql prompt as the postgres user

    sudo su postgres -c psql 
    
  3. Create a postgresql user for your project

    create user username with password 'password';
    
  4. Setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser

    alter user username superuser; 
    
  5. Create the development and test databases

    create database projectname_development;
    create database projectname_test; 
    
  6. Give permissions to the user on the databases

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

To end the postgresql session type \q

Update password for the user

alter user username with password ‘new password’;
Daniel
  • 2,950
  • 2
  • 25
  • 45
18

firstly, install postgresql

sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update

#now install postgresql
sudo apt-get install postgresql-9.1 libpq-dev
create a new user in psql
sudo su postgres
createuser user_name #Shall the new role be a superuser? (y/n) y
Gemfile
gem 'pg'

bundle install

development.yml
development:
  adapter: postgresql
  database: app_development
  pool: 5
  username: user_name
  password:
EricC
  • 1,355
  • 1
  • 20
  • 33
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
  • I also had to `create database app_development owner user_name;` and then `alter user_name password 'password';` – Connor Leech Nov 13 '13 at 13:08
  • 2
    no need to do it that way. use rake db:create, rake db:migrate . u can simply do rake db:setup to create and migrate. – Prasad Surase Nov 13 '13 at 13:14
  • execute createuser with `-W` to force a password prompt `createuser -s -W user_name`. Otherwise it may not ask you for one und you will have to set it afterwards with the `ALTER`-Statement – Axel Tetzlaff Jul 25 '15 at 10:03
2

You follow this link http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/

to create a postgres user and replace the credentials in database.yml

techvineet
  • 5,041
  • 2
  • 30
  • 28