Heroku Schema load: Permission denied for database "postgres" User - ruby-on-rails

I'm trying to deploy my Rails app to Heroku, and I now want to load my schema. I get the following error:
Running `bundle exec rake db:schema:load` attached to terminal... up, run.1533
rake aborted!
PG::ConnectionBad: FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.
Which is weird because postgres is not the database it should connect to. The database URL, which is available to the app via ENV, has the right values, and the long apparently random name d3kwlkwlwjwljetc....
Searching for this message gives me people who are trying to drop or reset the DB, which is not what I'm doing.
I have 0 rows
I tried pg:reset, even removed and put back the database. No avail.
I'm not doing db:drop, just db:schema:load
I'm dumbfounded. Why can't my app connect?
Edit: Apparently, I can't schema:load, but I can migrate all my migrations over. Why is that?

It appears that this is an issue in Rails itself - there is a discussion regarding it at https://github.com/rails/rails/issues/17945 - it seems that there's a commit (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb#L77) that is connecting to the posgres database which isn't permitted on Heroku so fails.
In short, for the moment use db:migrate until the issue is resolved.

Related

How to push local postgresql database built on rails to heroku?

I have some data in my postgresql database that I entered using a very simple rails app. I deployed it to heroku and now am trying to send my local database to heroku because its very empty right now.
At first I thought doing heroku run rake db:migrate would actually do this but turns out it only creates tables in heroku?
These are the steps I took to transfer my local database to heroku.
1: heroku run rake db:migrate ( I did this to create tables which should be empty right?)
2: heroku pg:push my_db DATABASE_URL --app my-herokuapp-98989 (the actual syntax to transfer database)
Here, it said
"Remote database is not empty. Please create a new database or use heroku pg:reset"
4: heroku pg:reset DATABASE
"WARNING: Destructive action" "postgresql-amorphous-59192 will
lose all of its data" "To proceed, type my-herokuapp-98989 or
re-run this command with --confirm my-herokuapp-98989
5: my-herokuapp-98989
"Resetting postgresql-amorphous-59192... done"
After this I try to open my app but it gives off an error saying
"ActionView::Template::Error (PG::UndefinedTable: ERROR: relation
"drinks" does not exist" "2017-07-29T04:50:20.697828+00:00
app[web.1]: LINE 1: SELECT "drinks".* FROM "drinks""
"drinks" is the name of my table and I think it is giving this error because I just reset my database so there is no table or columns and rows?
So again I try pushing my database to heroku and it just freezes at this point
6: heroku pg:push my_db DATABASE_URL --app my-herokuapp-98989
"heroku-cli: Pushing my_db ---> postgresql-amorphous-59192"
...and it freezes here
What am I doing wrong? Please help.
are you sure it's freezing? or its just loading, because you need to upload the dumpfile to heroku.
you also can try this command
heroku pg:backups:restore 'https://url/where/heroku/can/download/yourdbdump.dump' DATABASE_URL
more about import export you can read here. https://devcenter.heroku.com/articles/heroku-postgres-import-export#import

ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations

I have a local project, with rails and postgres. I threw it up on my AWS Amazon Linux AMI. I have run test projects with rails and postgres on the server. However when I uploaded my local project, and try to run
rake db:migrate
I get the following error:
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations
I saw some similar issues, but none of them help. I do have the proper role setup and connected.
I'm not sure if you're running rake db:migrate with RAILS_ENV as production or development. Whichever it is (development by default), in your config/database.yml it will say the user, password, and database it is running on. That user must have ALL privileges on the public.schema_migrations table. If it does, and it's still not working, make sure that user has ALL privileges on the public schema.
Read more about manipulating postgres database privileges here. Postgres has excellent documentation.
One more thing: if you're creating this database locally, and trying to create the initial database instead of actually running a migration, use rake db:schema:load instead of rake db:migrate. Never run this on production, as it will delete your data!

Rake db:reset 'user does not have CONNECT privilege'

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)

switching from Sqlite3 to PostgreSQL on an existing rails application

When I tried to switching from sqlite3 to postgresql in my existing application I faced this problem with rake db:migrate, I did the following
1 - rake db:create
2- rake db:migrate I got this error:
== AddColumn1: migrating ===================================================== -- add_column(:users, :first_name, :string) rake aborted! An error has occurred, this and all later migrations canceled: PG::Error: ERROR: relation "users" does not exist
3- rake db:reset
4- rake db:migrate , now migration is done with no errors
I have lost my data specially my admin user because of rake db:reset and my questions is :
1- why I forced to use rake db:reset ?
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
So, what you are saying is you lost data in your sqlite3 database? This should not have happened. Did you change your database.yml in config folder to point your app to the new Postgres DB? If you did point it to the new DB, you should not have lost any data in sqlite3 DB since the app is not hitting it any more.
If you have indeed changed your db config to use the new db, you may not have lost any data. I hope this is the case. :)
1- why I forced to use rake db:reset ?
I don't think you had to run rake db:reset on your new DB, unless your migration files are messed up.
2- is there ways to transfer my data from a database engine to another without losing it in the next time ?
You can create a rake task or a script that points to two db instances (the old one and the new one you are migrating to). You can then pull data from the old db and then massage data as needed (each RDBM has their own minor syntactical differences) and the load them to the new db.
3- and for PostgreSQL I couldn’t use a blank password , it said fe_sendauth: no password supplied , after adding password this error is gone , using password in another database engine instead of Sqlite3 is a must ?
It depends how you set up your Postgres DB instance you created...
4- will heroku require a reset also or its like Github will accept the data if I use another db engine in development ?
Setting up your DB on Heroku would be very simple. All you have to do is the following:
heroku crate your-app-name
git push heroku master
heroku run db:migrate
But, I think you have some issues in your migration files. Nevertheless, you can actually get a username/password for your DB on Heroku and then use your script to push the data over to it from your local DB.
I hope this helps!
If you are using Devise, you can reload your user account information using:
rake db:seed
Take a look on this guide https://www.digitalocean.com/community/tutorials/how-to-set-up-ruby-on-rails-with-postgres so you can recreate the process manually by changing your database.yml and Gemfile.
If you need to migrate your database information run
pgloader ./production.sqlite3 postgres://username:password#localhost/database
Check https://migara.li/2017/07/24/migrating-from-sqlite-to-postgres/
Other alternatives like taps aren't working right now
http://railscasts.com/episodes/342-migrating-to-postgresql

rake aborted! Permission denied - rsa_key

Trying to run db:migrate
keep getting rake aborted
== CreateLocalKeys: migrating ================================================
rake aborted!
An error has occurred, all later migrations canceled:
Permission denied - rsa_key
I can't find anything about this
any help is appreciated
Thanks
Are you trying to install Insoshi? If so, I would assume the user account running the migration does not have write access to the local filesystem. The particular migration writes keys to a local file as seen here: http://github.com/insoshi/insoshi/blob/master/db/migrate/012_create_local_keys.rb
I'm assuming your user account needs write access in your rails root directory (at a minimum). Probably a little chown/chmod love will help.
sudo rake db:migrate
First thing I can think of. But I've never had a problem running db:migrate.

Resources