Working on a rails app locally with a remote Postgres connection?

10,668

Solution 1

Below are the steps to access Heroku db from local development:

  1. Login to your heroku account.

  2. Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url

  3. Select your application database.

  4. You will able to see your heroku pg credentials:

    Host         xxxxxxxxx.77777.amazonaws.com
    Database     42feddfddeee 
    Username         44444444444
    Port         xxxx
    Password     777sfsadferwefsdferwefsdf
    
  5. collect above details and put in your databse.yml file development evn:

    adapter: postgresql  
    host: < host >                            # HOST 
    port: < port >                            # Port  
    database: < database name >               # Database Name  
    username: < user_name >                   # User Name  
    password: '< password >'                  # Password 
    

Restart you application,

Best of luck..!!

Solution 2

I am not a postgres user but this should work. You can alter your database.yml to include host and port

production:
  adapter: postgresql
  encoding: unicode
  database: di_production
  pool: 5
  username: user
  password:
  host: heroku.host.name
  port: <postgres listen port>

And of course, you should allow connection on the server side, both at the firewall level and database level.

A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI

DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect  # or what ever method you want to print it

Solution 3

I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.

From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate

From my config/dtabase.yml

development:
  adapter: postgresql
  encoding: unicode
  database: di_development
  pool: 5
  username: davidlee
  password:
test:
  adapter: postgresql
  encoding: unicode
  database: di_test
  pool: 5
  username: davidlee
  password:
production:
  adapter: postgresql
  encoding: unicode
  database: di_production
  pool: 5
  username: user
  password:
  • and it works. I can't remember doing anything else.

Pierre

Share:
10,668
dsp_099
Author by

dsp_099

Updated on July 27, 2022

Comments

  • dsp_099
    dsp_099 almost 2 years

    Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?

    I'm having trouble understanding how Heroku, Rails and PG gem work together.

    It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?

  • dsp_099
    dsp_099 over 10 years
    Thanks. For anyone reading this: Take care to use "username:" and not "user:" in database.yml or it will try to connect you with your system name