0

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?

2
  • 1
    Have you completed Rails Guides at guides.rubyonrails.org ? Commented Jan 9, 2016 at 20:37
  • Perhaps some code would be helpful here? Commented Jan 9, 2016 at 20:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, Peter! I will try ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.