Rake db:reset 'user does not have CONNECT privilege' - ruby-on-rails

So I'm using Heroku Postgres in my Rails app, but I'm not hosting my app on Heroku itself. I used the Active Record connection details from Heroku in my database.yml, and it looks like this:
development:
adapter: postgresql
encoding: unicode
pool: 5
database: [database]
username: [username]
password: [password]
host: ec2-54-227-243-78.compute-1.amazonaws.com
port: 5432
However, now I'm trying to rake db:migrate my app so the database gets all set up with my models. Running that command doesn't do anything, so I tried rake db:reset and I get this:
Couldn't drop df2cokjfj0k4vu : #<PG::Error: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
df2cokjfj0k4vu already exists
-- initialize_schema_migrations_table()
-> 1.3997s
-- assume_migrated_upto_version(20130924040351, ["/home/action/braindb/db/migrate"])
-> 0.0882s
Any idea what I'm doing wrong. I'm still pretty new to Rails so sometimes I forget how to get my Postgres database setup properly when migrating hosts.

Use heroku pg:reset DATABASE instead as noted in https://devcenter.heroku.com/articles/rake
You cannot drop databases in Heroku with rake db:reset because user has no privileges.

You can't drop your PG database on Heroku.

I recently had this problem and resolved it through the following steps.
heroku pg --help gets the name of commands for using postgres
heroku pg:reset DATABASE # resets the db
Answer the prompt to confirm

Like others I had a similar problem but running pg:reset did not help: that's where I ran into the user does not have CONNECT privilege issue.
1) heroku pg:reset you will be prompted to enter the project name to confirm.
2) From Heroku's docs run heroku rake db:schema:load

I had the same problem. I fixed it by running:
heroku pg:reset DATABASE
(note: must specify DATABASE as above)

If you got this because you were running rake db:create, instead, simply skip to the migration. For whatever reason, postgresql on heroku doesn't require the rake db:create step as may be required on local installations.
In other words, instead of
heroku run rake db:create db:migrate db:seed
run this instead
heroku run rake db:migrate db:seed

The solution to me was to just go straight to migration because the database is already created (don't go on "heroku run rails db:create")
heroku run rails db:migrate -a YOURAPPNAME

For one of my apps, upgrading to the first paid tier of a Heroku database seemed to work for me: https://devcenter.heroku.com/articles/upgrade-heroku-postgres-with-pgbackups#provision-new-plan
Reason was that 'heroku pg:info' revealed that I was over my row limit as shown here:
Rows: 12392/10000 (Write access revoked)

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

rake db:create - Mysql2::Error: Unknown database

I am trying to create a database using rake db:create task.
When I am running the command bundle exec rake db:create to create the database and load the schema, I get the following error:
rake aborted!
Mysql2::Error: Unknown database 'xxx_development'
My database.yml:
development:
adapter: mysql2
host: localhost
reconnect: true
username: user
password: password
pool: 50
database: xxx_development
I checked that mysql server is running and I am able to connect to it using the password/username I have in the database.yml
I also understand that I can go ahead and create the database in mysql and then run the bundle exec rake db:create but isn't the rake task db:create also creates a database in case it doesn't exists?
The rails version is 3.2.22.
and mysql2 version is 2.9.13.
Any pointers will be highly appreciated.
If you are using ohmyzsh with bundler plugin which makes rake run by default with bundle exec, you can use
unbundled_rake db:create
Ran into a similar issue on Rails 5 and the following steps helped me fix it:
Spring stop
./bin/rails db:create
I have two ideas.
Check the User permissions
When I run rake tasks, I don't use bundle exec (i.e just rake db:create)
Hope this is helpful
Try rake db:create RAILS_ENV=development hope this will work
For some reason rails db:create failed but my workaround was
to create dbs directly in mysql
sudo mysql
create database myapp_development;
create database myapp_test;
After that run as normal
rake db:migrate db:seed

Local sql database and heroku postgres database files differ, how to sync again? Ruby on Rails

I am currently doing the Ruby on Rails tutorial by Michael Hartl. Somewhere along the way I messed up my database. In my database file there is only 1 user, by the name of Bob.
Locally in cloud9 IDE, when I do 'rails console' and then do Users.first, I get a user with a name of "Bob".
However when I do 'heroku rails run console' and do Users.first, I get a user with a different name. (I probably changed the name somewhere along the way)
How do I get Heroku to see the correct local database file again? Should I clear the heroku database, then use pg:pull to pull the local sql database to heroku?
Not sure if you've gotten to Chapter 9 yet but section 9.3.2 of the current book deals with creating sample users. This is done through the db/seeds.rb file.
Running $ bundle exec rake db:reset then resets your DB followed by $ bundle exec rake db:seed to fill it with your new data.
You can run the same procedure on your production application with:
heroku pg:reset DATABASE
heroku run rake db:migrate
heroku run rake db:seed
It is, of course, also possible to transfer data between local and production databases with tools such as heroku-pg-transfer but that's a little advanced if you're only starting out, and I think somewhat unnecessary if you only have one user to transfer over.
Hope this helps.
You can use yaml_db gem to dump your local data to file and then upload it to heroku.
On your development machine:
rake db:data:dump
Then commit changes, push to heroku and run:
heroku run rake db:data:load

Resetting database on heroku

Now I am trying to reset database on heroku but got stuck.
I know I should do "heroku pg:reset Database URL but I don't know exactly what the databese url is.
I did "heroku config". So I got some strings. And tried
heroku pg:reset postgres://ffsqtmlnxntfvl:ajqGTMGEmT7U1S6sVdi7-bb7Cm#ec2-107-20-245-109.compute-1.amazonaws.com:5432/dc2kahsue8hn20. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_AQUA_URL
My command,however,always returns this to me.
Unknown database: postgres://xxxxxxxxx:xxxxxxxxxxxxxxx#xxx-xxx-xx-xxx-xxx.compute-1.xxxxxxx.com:xxxx/xxxxxxxxxxxxxx. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_AQUA_URL
What should I do? Could you give me some advice?
You should use:
heroku pg:reset HEROKU_POSTGRESQL_AQUA
I know this question is old, but as I've just been through this, here is what I did:
heroku pg:reset HEROKU_POSTGRESQL_HEROKUCOLOR_URL --confirm {app_name}
heroku run rake db:schema:load
heroku run rake db:migrate
heroku run rake db:seed
As per: Heroku rake db:migrate does not create tables (Rails 5)

Heroku won't reset my database

I have been trying to run $ heroku pg:reset from the command line but I believe I'm not putting in the database correctly. I've tried a number of variations.
I ran $ heroku config | grep POSTGRESQL to get the database name which prints as
HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
I've tried running everything from
#1
$ heroku pg:reset HEROKU_POSTGRESQL_PINK_URL: postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#2
$ heroku pg reset postgres://naknaswvxfvuup:QK2dHYNMZ_va10lDgMDo4S0BIM#ec2-23-21-161-255.compute-1.amazonaws.com:5432/db7eute4gu4mcb
#3
$ heroku pg:reset db7eute4gu4mcb
and other variations. Please let me know how to correctly note this as I keep getting either an error or this text from the commmand line " ! Unknown database: db7eute4gu4mcb. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_PINK_URL"
I'm currently at 10.4 on the Ruby on Rails tutorial. Thanks!
You should specify a DATABASE when run heroku pg:reset. This is the syntax:
heroku pg:reset <DATABASE>
To know the value of , you can run:
heroku pg:info
It will return DATABASE_URL, something like: HEROKU_POSTGRESQL_GRAY_URL
Then you can reset your database:
heroku pg:reset HEROKU_POSTGRESQL_GRAY_URL
In your case, to reset database run:
heroku pg:reset HEROKU_POSTGRESQL_PINK_URL
this is how it worked for me
(replace app-staging with your app's name, do not replace DATABASE_URL, this is how heroku now finds the app's db)
heroku pg:reset DATABASE_URL --confirm app-staging
hope it helps
Assuming you have your authentication information exported to the shell environment, you should be fine with just passing the name of the database. For example:
PGPASSWORD='foobarbaz'
export PGPASSWORD
heroku pg:reset pink
There are certainly other ways to use the reset command, but IMHO this is the easiest.

Resources