Postgres db not working on Heroku - ruby-on-rails

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!

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 -
Heroku Getting Started Guide
Heroku Postgres Addon Guide

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:
add the Heroku add-on (such as Heroku Postgres). Add-ons are located here: https://elements.heroku.com/addons.
rake db:migrate
rake db:seed
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.

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

Related

Added to Rails app on Heroku. How to setup database in local environment?

I was recently added to a Rails application via Heroku. I'm able to clone the app to my local machine using:
heroku git:clone -a appname
However, I have issues running local tests and other tasks related to not having a local database. The production environment uses Postgres and the development environment uses sqlite3. What's the best way to get a local database setup?
Simply pull the production database from Heroku. Run the following once you have heroku_toolbelt and you have access to the app on Heroku:
heroku pg:pull DATABASE_URL app_development --app app_name_on_heroku_here
This will create a new database locally named "app_development" so rename it to your app. See config/database.yml for your development database name.
You will need to setup the databases required for development. This can be done using the built in rake db tasks:
rake db:create
rake db:migrate
This should see your local database created and migrated up to the latest migration.
As you have successfully clone rails project from Heroku and your production database is PostgreSQL which is different from your local database SQLite you should update some files from your local project
1. remove or comment "gem pg" and add gem 'sqlite3' and run bundle install
2. Make changes in your database.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
3 run rake db:create
4 run rake db:migrate
5 if you have data in seed file run rake db:seed

Rake db:reset execute in development and test environment

I got a Ruby on Rails v5 application and I'm trying to run the following Rake task in development environment:
rake db:reset
Sadly, Rake runs the task in both development and test environment. Since I don't use the test environment, I don't wanna double my database. I have read somewhere that Rake run task in both environment when there is no RAILS_ENV defined. So I try to add the following line to my .bash_profile without any success:
export RAILS_ENV="development"
I also tried to add RAILS_ENV=development at the end of my task but it also did not worked.
Is there a way to use Rake in development only ?
Update #1
Thanks to Taryn East for the quick comment. I'm trying to update my post as quick as possible to make it easy for you to answer efficiently.
What do you actually observe when you run that command?
The command are simply executed twice. Since I have different database for my development and test environment, it does not show me any error at the moment. On the other hand, it does force me to use two database for no reason at all. I also tried to set the same database for the two environments, but then I was getting error since Rake tried to run the task twice on the database.
eg of output for rake db:drop
Dropped database 'db'
Database 'db' does not exist
It did drop my database, but as you can see Rake tried to run the command another time for the other environment.
Why aren't you using the test environment?
Because I don't see any advantage of it for my current project. I don't have time to create and update tests. I'm also not used to use both development and test environment at the same time.
If you want to run a rake task in just a single environment, you can also add RAILS_ENV to the command line.
As I said in my post, it does not work. Here's what I did:
RAILS_ENV=development rake db:drop
And here's the output:
Dropped database 'db'
Database 'db' does not exist
Update #2
Here's my database.yml configuration:
default: &default
adapter:mysql2
pool: 5
timeout: 5000
development:
<<: *default
database: db
username: something
password: something
host: localhost
port: 3306
test:
<<: *default
database: db_test
username: something
password: something
host: localhost
port: 3306
I don't know if it can help, but my environment is development according to the rake about task.
I have read somewhere that Rake run task in both environment when
there is no RAILS_ENV defined.
This is not correct. If the environment is development, test is also added to current environments. See: https://github.com/rails/rails/blob/f507085fa2a7625324e6f4e3ecef9b27dabefb4f/activerecord/lib/active_record/tasks/database_tasks.rb#L339
So this is expected behaviour.
On the other hand what you're saying will be true for any other env. E.g. RAILS_ENV=test rails db:drop will only execute for test env.

Setting up PostgresQL on Heroku Rails 4

its been a while since i have used postgresql and deployed an app to Heroku, think I have made an error somewhere with my setup.
I have created my app on Heroku, there is a Hobby Dev database setup (I ran Heroku run rake db:setup) which set up my database, but I'm wondering in my database.yml file do i have an error
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
When I run heroku run rake db:create i get
FATAL: permission denied for database
DETAIL: User does not have CONNECT privilege.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"databaseName", "username"=>"userName", "password"=>"password", "port"=>5432, "host"=>"hostNamehere"}
I have set the database password using heroku config:set
What have i missed here?
Thanks
Heroku ignores your database.yml configuration, they generate one when you deploy your application and they also take care of database creation.
All you need to do is heroku run rake db:migrate and maybe a heroku run rake db:seed in case you need to seed your database.

Adding a production database to a Rails 4 site on Heroku

Well I've deployed my site on Heroku. When I navigate to the app page it shows a database listed on the Add ons section
I navigate ahead to the database details and this is what I get to see
This is my production database code
production:
adapter: mysql2
encoding: utf8
database: dfu332icuvq6bs
pool: 5
username: cekapynykihldm
password: b1XuQq9m1m8Ok3zeyABEgNiiz3
socket: /tmp/mysql.sock
My question is when I fill in the details from Heroku into the production database and push it bask to the server, the app doesnt talk to the database as none of my routes except the home#index works. How do I make my app talk to the database on Heroku.
From your comment, it appears that you need to run the database migrations and then restart your instance:
heroku run rake db:migrate
heroku ps:restart
Guess I missed the most important step. I was able to fix the problem by running
heroku run rake db:migrate

Elementary Rails deployment

I am trying to deploy my app on Rails for the first time using Heroku.
I have spent quite a lot of time but there's a gap somewhere.
Git: created private repo, pushed to git successfully
Heroku: created free app and pushed successfully (but app 'crashes')
Local:
rake db:schema:dump #success
rake db:schema:load RAILS_ENV=production #failure: production database is not configured
rake db:create db:load RAILS_ENV=production #failure: undefined method '[]' for nil:NilClass
active_record/railties/databases.rake:59:in 'rescue in create_database'
active_record/railties/databases.rake:39:in 'create_database'
My database.yml file:
defaults: &defaults
adapter: mysql
username: root
password: password
host: localhost
development:
<<: *defaults
database: project_dev
test:
<<: *defaults
database: project_test
Just added:
production:
<<: *defaults
database: project_production
I may be making a total rookie mistake. Do you know where I might be going wrong?
Use the command heroku rake db:schema:load, which simply executes the command rake db:schema:load on Heroku's environment.
You do not need to worry about the database environments are they are automatically configured by Heroku on the compilation of the slug.
The rake commands which you are running, run on your development machine. If you want to run rake commands on the server, use the heroku command (example):
heroku rake db:create
Note, if you want to push data, you are doing it wrong. Go to heroku.com and look at the docs there.

Resources