David Lexa
David Lexa

Reputation: 199

Ruby on rails: Connecting to external Postgresql database

I'm new to Ruby on rails. I created my rails project and I would like to connect to an existing postgresql database (of company I work for) and display then some data in my web app. Can anybody help out how to do that?

Upvotes: 0

Views: 2808

Answers (1)

Peter Black
Peter Black

Reputation: 1172

These directions assume you are using some version of Linux. However, they would be very similar on other operating systems.

Add the 'postgresql' gem to your Gemfile:

 gem 'pg'

Then open a terminal window in the root directory of your application and run:

 bundle install

Edit postgresql.conf (located on the remote postgresql server) and find the line that reads:

 #listen_addresses = 'localhost'

Remove the comment and change it to:

 listen_addresses = '192.168.0.14, localhost'

Replace '192.168.0.14' with the ip of your Rails application.

Now open pg_hba.conf (located on the remote postgresql server) and scroll down to:

 # Put your actual configuration here

Directly below that enter your configuration like so:

 # TYPE   DATABASE      USER        ADDRESS        METHOD
 local    all           all         localhost      md5
 host     all           your_user   192.168.0.14   md5

After saving both of those files run the command:

 sudo service postgresql restart

Now edit your Rails application's config/database.yml:

 production:
   adapter: postgresql
   encoding: utf8
   database: the_database_name
   username: your_user
   password: your_database_password
   host: 192.168.0.14
   port: 5432
   pool: 10

 development:
   adapter: postgresql
   encoding: utf8
   database: the_database_name
   username: your_user
   password: your_database_password
   host: 192.168.0.14
   port: 5432
   pool: 10

Change 'the_database_name', 'your_user', and 'your_database_password' to the appropriate values.

After that, you should be good.

Upvotes: 2

Related Questions