简体   繁体   中英

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.

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  

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

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

DigitalOceanCommunityArticle Click Here

AskUbuntuLink Click Here

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM