Abid Hussain
Abid Hussain

Reputation: 7762

How to connect to Postgresql from Ruby On Rails App

I am new in Ruby on rails and i am using ruby v 1.9.3 & rails 3.2.12 with window 7

i want to PostgreSQL database connection in my app.

this is my db connection

development:  
  adapter: postgresql
  encoding: unicode
  database: mmagap_development
  pool: 5
  timeout: 5000
  host: 127.0.0.1
  username: postgres
  password: root

test:
  adapter: postgresql
  encoding: unicode
  database: mmagap_test
  pool: 5
  username: postgres
  password: root

production:
  adapter: postgresql
  host: 127.0.0.1
  encoding: unicode
  database: mmagap_production
  pool: 5
  username: postgres
  password: root

this connection every time give error

D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in
`initialize': received invalid response to SSL negotiation: - (PG::ConnectionBad)

Please help how to create PostgreSQL database connection in my RoR app.

Upvotes: 0

Views: 8836

Answers (5)

Vishal Patidar
Vishal Patidar

Reputation: 83

First you should install PostgreSQL and PgAdmin

After installing this thing you will have to add some pic of code ( to connect PostgreSQL to your rails project ) in your project config/database.yml file (before add PostgreSQL code you will have to remove all Sqlite3 code from config/database.yml file).

development:
  adapter: postgresql
  encoding: unicode
  database: database_name
  pool: 5
  host: localhost
  username: your_PostgreSQL_username
  password: your_PostgreSQL_password

test:
  adapter: postgresql
  encoding: unicode
  database: database_name
  pool: 5
  username: your_PostgreSQL_username
  password: your_PostgreSQL_password

staging:
  url: <%= ENV['DATABASE_URL'] %>

production:
 url: <%= ENV['DATABASE_URL'] %>

postgres is default username and password in PostgreSQL database\

after add this pic of code you have to add one line of code in your rails project root directory in Gemfile

gem ‘pg’

and you must have to remove sqlite3 code from Gemfile ( gem 'sqlite3', '~> 1.4' )before adding gem 'pg'

Upvotes: 0

Jenorish
Jenorish

Reputation: 1714

Removed host from development config in database.yml,its worked for me.

development:  
      adapter: postgresql
      database: tes
      pool: 5
      timeout: 5000
      username: postgres
      password: root

Upvotes: 0

Sugat Mankar
Sugat Mankar

Reputation: 478

First install pg gem

    gem install pg

your apps database.yml file shoud be like this

development:   
    adapter: postgresql  
    encoding: unicode  
    database: myapp_development  
    pool: 5  
    username: myapp  
    password: password1  

test:  
  adapter: postgresql  
  encoding: unicode  
  database: myapp_test  
  pool: 5  
  username: myapp  
  password: password1

Now run

rake db:setup    
rake db:migrate

and after this run

rails s  

Upvotes: 2

RahulOnRails
RahulOnRails

Reputation: 6542

Use this link to verify, Is Postgresql installed successfully or not.

DigitalOceanCommunityArticle Click Here

AskUbuntuLink Click Here

Upvotes: 0

Alok Anand
Alok Anand

Reputation: 3356

You can change host: 127.0.0.1 to host: localhost. It should solve the issue.

Upvotes: 2

Related Questions