Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 563
dsp_099 Avatar asked Oct 21 '13 04:10

dsp_099


3 Answers

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..!!

like image 52
Pravin Mishra Avatar answered Sep 21 '22 01:09

Pravin Mishra


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
like image 35
Litmus Avatar answered Sep 18 '22 01:09

Litmus


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

like image 45
user1854802 Avatar answered Sep 18 '22 01:09

user1854802