2

I've spend several hours figuring out how to get my database up and running. I created a new rails app and wanted to deploy it to heroku. I followed the instructions from heroku (to switch from sqlite3 -> postgresql) but it just doesn't work.

This is in my database.yml file:

default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  url: <%= ENV['DATABASE_URL'] %>

I can't create or seed any data in the database. Sometimes it executes the db:migrate, but even then it doesn't create anything. This is what I get when running:

heroku run rake db:create

FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.

Does anyone has an idea on how to solve this? I don't have a clue anymore ...

Thanks!

4
  • in your Heroku dashboard. Resources -> addons. Have you added Heroku Postgres :: Database add-on ? Commented May 8, 2018 at 0:05
  • Yes, I added the Postgres addon! Commented May 8, 2018 at 8:42
  • Have you chosen a web Dyno (the one for free is ok) and activated it ? Commented May 8, 2018 at 11:19
  • Yes. I found a solution by re-adding the Postgres addon on heroku. Now it works fine. Thanks for your help! Commented May 9, 2018 at 7:34

3 Answers 3

3

By default you don't need to create a db on heroku. Just run heroku run rails db:migrate and rest of the stuff will be handled by heroku itself.

Also your database.yml should be changed to following for Production env.

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Also your rails app is by default assigned with a Postgres addon. You can check this by running command heroku addons in the console.

Source -

  1. Heroku Getting Started Guide
  2. Heroku Postgres Addon Guide
Sign up to request clarification or add additional context in comments.

Comments

3

As per the given stacktrace, it seems like you are trying to create a database on heroku which in turn is giving you Permission Denied Error.

Firstly, you do not need to run

heroku run rake db:create

Instead run

heroku run rake db:migrate

and it should migrate the migrations which are down.

For checking the current status of migrations run the following command:

heroku run rake db:migrate:status

Other Point you mentioned:

-> I can't create or seed any data in the database As already mentioned above you can't create a database as heroku does it for you . For seeding data in the database run the following command:

heroku run rake db:seed

Comments

2

You cannot create a database on Heroku using db:create (you cannot drop it neither). Your database is created when you add an add-on (such as Heroku Postgres). You can only migrate and seed. And if you want to start over, you can use pg:reset (instead of drop and create)

So the correct sequence should be:

if you want to start over

  • rake pg:reset
  • rake db:migrate
  • rake db:seed

From Heroku documentation: https://devcenter.heroku.com/articles/heroku-postgresql

The PostgreSQL user your database is assigned doesn’t have permission to create or drop databases. To drop and recreate your database use pg:reset.

1 Comment

Thanks, I re-added the postgres addon and now it works fine! Cheers

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.