Setting up PostgresQL on Heroku Rails 4 - ruby-on-rails

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.

Related

Postgres db not working on Heroku

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

rails is not using the configuration set on database.yaml

This is my database.yml
production:
adapter: mysql2
encoding: utf8
database: testing
pool: 5
username: ubuntu
host: 127.0.0.1
When I run rake db:setup I got this:
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"testing", "pool"=>5, "username"=>"ubuntu", "host"=>"127.0.0.1"}
-- create_table("documents", {:force=>:cascade})
rake aborted!
Mysql2::Error: Access denied for user ''#'localhost' to database 'testing'
Why it's not using my username and host? instead is showing '' # 'localhost'
I think, database.yml config for production environment, but you run it on development environment. You can use 'RAILS_ENV=development rake db:setup' or 'RAILS_ENV=test rake db:setup'
One more thing, database should be created by mysql user with root privileges

Problems with heroku rake db:reset

I have a problem, I pushed my app to github and I made the deploy with heroku, everything was ok, but when I run heroku run --app myapp rake db:reset (to populate the database on heroku) I found the following messages filled with errors (I show only the part that I think is important because the message was too long)
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
.
.
.
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Couldn't drop d5q6ajst4p4d97
FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
.
.
.
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `load'
/app/vendor/bundle/ruby/2.0.0/bin/rake:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "database"=> [DATABASE], "pool"=>5, "username"=> [USERNAME], "password"=> [PASSWORD], "port"=>5432, "host"=> [HOST]}
-- enable_extension("plpgsql")
-> 0.0370s
-- create_table("heros", {:force=>:cascade})
-> 0.0307s
-- initialize_schema_migrations_table()
-> 0.0579s
Note: I'm hiding the username, password, etc...
Everything works fine in the deployed app, but I find ugly to see so many mistakes when making a rake db: reset in heroku, so after seeing this I searched how to solve this problem and found some posts that preach the same solution, so I followed the recommendations and run the command heroku pg:reset DATABASE_URL --app myapp, it ended succesfully, but the same error message appears when I tried again to run rake db:reset, What could be happening?
This is my database.yml
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username:
password:
test:
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username:
password:
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username:
password:
Heroku, being a shared service is not allowing users the permissions to drop nor create databases - hence the error when running rake db:reset
Hence heroku pg:reset is present as a command to perform the reset of your database.
See https://devcenter.heroku.com/articles/rake
I got the same error I have fixed it by
heroku pg:reset DATABASE_URL
heroku rake db:migrate
As per you error
Also your database.yml point postgres user to connect database in heroku
it should not be postgres as a user in production mode.
so
Active Record 4.1+ Escape Valve
If you need need to connect to a different database than the
ENV['DATABASE_URL'] Rails 4.1+ supports a URL key in the database.yml
file that will take precedence.
production:
url: <%= ENV["SOME_OTHER_URL"] %>

database.yml file configuration and postgres - rake db:drop db:create db:migrate

I recently switched to postgres for a rails app in development since I deployed on Heroku and want to match production. Postgres is installed and working correctly for my application when I set the database name equal to 'postgres', which is the default name from what I understand.
However, I would like to change the database name for development, test and production. If I simply change the names in database.yml, and try to run a rake task, I get the following error in my terminal:
NOTICE: database "something_development" does not exist, skipping
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?>
rake aborted!
FATAL: database "something_development" does not exist
Here is what my database.yml file currently looks:
development:
adapter: postgresql
host: localhost
database: something_development
username: name
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: name
database: something_production
Are there some other steps I am missing? Apologies if I am missing something obvious here! Thanks!
Changing the name in the database.yml file only changes the place that it looks for the database, it does not actually rename the database itself. If you change the specified location, you will need to rename the database to match, using a GUI tool (like navicat or pgadmin) or the command line.
Also, it is probably an obfuscation omission, but your posted output refers to both shredset_development and something_development. If this continues to fail, perhaps verify that all of your names are the same everywhere...

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