4

I'm totally new to Heroku and Postgres and I'm trying to figure out how to setup and access the Postgres db in a Heroku Ruby app.

I'm not sure how to go about setting this up. I've found some information about using the command:

rake db:create

Where do I enter this command? I'm completely in the dark on this.

Any help with how to setup/access the Postgres db in Heroku would be greatly appreciated.

Thanks.

2
  • You said this is for a Ruby app, but the code you sampled is Rails specific. Is this for a Rails app? Commented Jan 4, 2013 at 22:54
  • I just found that example, but it is a ruby app. Commented Jan 4, 2013 at 23:52

2 Answers 2

2

Without info on what type of Ruby app you're building, what gems you may be utilizing, or what frameworks you may be building on, it is impossible to completely guide you through how to connect your Ruby app to the Heroku Postgres database. But here are a few things to point you in the right direction:

  • Install the Heroku Toolbelt as @jordan.baucke suggested. Beyond just adding plugins, you'll be using this toolbelt for nearly every Heroku-related action. Just follow the link, download, and install. Easy!
  • Now that you have the toolbelt, login to your account from the command line: heroku login.
  • Now make your app. While in your app folder (on the command line), execute: heroku apps:create <app name> -s cedar.
  • Now add the Postgres db: heroku addons:add heroku-postgresql:dev -a <app name>
  • From Ruby, you can connect to the db through the environment variable: ENV['DATABASE_URL'].
  • Deploy the app via git: git push heroku master.

From here, we really can't give you any further guidance since we don't know how you're interfacing with Postgres. But the above steps should at least get you to be able to connect to the db from your Heroku app.

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

5 Comments

Thank you for the steps listed. I did install the Heroku Toolbelt and can successfully logon. For the additional steps. An app has already been created called "nameless-ridge-8571" and under add-ons I have Heroku Postgres Dev:: Jade. Am I correct that the app is already created and the Postgres database has been added as well? So, do I only need to connect to the db through the environment variable and then deploy? Where do I obtain the database URL? I see the connection string section in Heroku cloud. Thanks again for the help. I sincerely appreciate it.
Yes, the app and db are ready. The environment variable ENV['DATABASE_URL'] is a string containing all of your db credentials. For Rails apps, Heroku will generate a new database.yml with those credentials. Since you're building a non-Rails app, you'll need to parse the string to extract the info. Check out this Heroku doc to see some examples of how to do this.
I'm confused about the ENV['DATABASE_URL']. Should I be running that from the command line? I'm not clear on that? I've logged into HerokuPostgres and can see the app and the db. From the command prompt I can run heroku login and successfully login. I can also run the command 'heroku apps' and get the same app name and the email of the person in our group that setup the app. Logged into HerokuPostgres, I see the database addon, so the app and db are ready. This is where I'm confused on the next step with the environmental variable? Thanks for the help.
ENV['DATABASE_URL'] is actual code you'd use in Ruby. Have you taken a look at the docs I've linked to? They will show you examples of how to implement. Basically, db = URI.parse(ENV['DATABASE_URL']. Then you can reference values like db.host, db.user, db.password, db.path[1..-1], etc. If you'd like to see that string for yourself, execute heroku pg:credentials DATABASE from the command line. And check out this doc and this doc - they have answers for you.
ah, okay, just got the credential info by running: heroku pg:credentials jade --app nameless-ridge-8571 Okay, so I see the creds now.
1

For starters, are you using the the Heroku toolbelt? (command line tools?)

You can do it from the website as well, but with the toolbelt, you can enter heroku addons:add heroku-postgresql:dev --app *your app name*

Heroku will automatically inject the database into your app's database.yml when you deploy your app into Heroku.

Finally, you need to migrate the production database to your current migrations. You can do this remotely with the toolbelt again:

$ heroku run rake db:migrate

This will remotely connect a console and pull your migrations into your production database on the server.

1 Comment

Hi Jordan, thanks for the reply. I'm not using Heroku toolbelt. How can I do it from the website? If that is not possible, I can download the toolbelt and try it from there. Thanks for your help.

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.