I've setup a system to automatically download and store a db dump from my Heroku deployed rails app. Everything is working great but when I download the dump and restore it to my local postgres server then try and run my local app off that restored database I get this error
ActiveRecord::StatementInvalid in HomeController#index
PGError: ERROR: permission denied for relation users : SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Anyone have any suggestions on what that could be? I've checked my postgres permissions and all tables and the database itself belong to the postgres user. I've tried GRANT ALL as well with no success.
Go through your pgdump.sql files and remove the lines after each CREATE TABLE statement that say something like:
ALTER TABLE public.users OWNER TO eqrunyvndu;
Then run your restore from that and it should work. Those lines change the owner of the tables to your heroku app's autogenerated db username, which is meaningless locally, so you can just remove them.
I'd also recommend greping for any other occurrences of that username and removing those too.
Most probably the PostgreSQL user you are using is different one that is owner of this table.
I suppose you have different user on that database and on this one?
Related
In my rails app, I am using postgresql as my adapter in database.yml file. I want to rename the database and reassigned them to a different owner. So i went ahead and changed my postgres database by running:
ALTER DATABASE old_name RENAME TO new_name;
CREATE ROLE new_role WITH PASSWORD 'pw123';
ALTER USER new_role CREATEDB;
ALTER USER new_role LOGIN;
Then I also modified my database.yml file accordingly
After all that, when i run the app again, the error: InsufficientPrivilege: ERROR: permission denied for relation schema_migrations
This new role has the exact same privilege of the previous owner, but I don't know why it is yelling at the new owner.
I believe I am missing a tiny thing here, but i would not figure out what. So what should i do to let my app accept the new postgresql changes?
The database has changed ownership, but the objects in it have not. So the schema still belongs to the old user, and the new user has no permissions on it.
When it comes to permissions and ownership, databasea are objects like all other objects. To use an analogy, you don't own everything in a house just because you own the house.
The REASSIGN OWNED SQL command can change ownership of all objects in a database.
I'm not sure if I'm going mad or if Heroku's dataclips are acting up but I have the following problem.
I've set up a ruby on rails development environment on Nitrous.io and have connected to a database on Heroku. In Nitrous.io I then type psqlto access postgresql.
From there I type \lto list the databases.
Then \c <name of development database hosted on heroku>
Then \dt to list the relations, one of which is users
Finally, I input the command SELECT * FROM users; and this returns relevant information on users such as email address, encrypted password etc etc.
However, when I go to Heroku and create a dataclip with the code select * from usersto be run against the same database, I'm getting the following warning
Error: Dataclip cannot be created
ERROR: relation "users" does not exist
LINE 2: select * from users
Am I completely missing something here or is the dataclip throwing an error for no reason? I'm using Devise (for the first time) in my app. Would this have anything to do with it?
Edit
I'm starting to think I've stored the database on Nitrous.io and not in Heroku. I think I may have used autoparts to install postgresql on my nitrous.io vm
I've run the command SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' as a dataclip on Heroku and the users table just isn't there. I then ran it on some other projects I have on Heroku and I can clearly see the tables I've created.
Can anyone confirm my suspicion or know how I can check the location of the database?
Yes I had indeed set the postgresql database up in nitrous.io itself rather than on Heroku. This page explains it: http://blog.nitrous.io/2013/07/02/building-a-rails-4.0-app-on-nitrous-io.html
I have two users in postgresql one of which is called migration and is used when Rails runs migrations on the production server. This user owns the production database. I also have production user who is supposed to have only the following privileges: SELECT, INSERT, UPDATE, DELETE on the production database.
Problem is, every time a new table is created, I have to manually run this in psql:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO production;
-- next line is needed for each new table which has an auto incrementing field, in this case - table `users`
GRANT USAGE, SELECT ON SEQUENCE users_id_seq TO production;
because permissions for production user on newly created tables are not set automatically. What's the best way to do it automatically when running migrations? Any script available for Rails/Capistrano?
You could use Postgres' ALTER DEFAULT PRIVILEGES to have it automatically assign the rights to production for all newly created tables.
Alternatively, you could write a custom Capistrano task to set the permissions that is called through the after "deploy:migrate", "mycustomtaskname" hook. This pastie might give you a few good hints on how to interact with pgsql through Capistrano, for example how to provide the password interactively.
I'm a Rails novice, but am currently trying to modify an existing application that I inherited.
I'm running some db migrations that create new tables on our staging server (Not local machine) and all the new DB tables create seem to have different permissions than the tables already created.
I am running "rake db:migrate VERSION=20110213100531 RAILS_ENV=staging" with one sshlogin and I have my sql login defined in my database.yml under staging as separate mysql database login.
Is there any particular settings/user I should be running my db migration under?
From what you've said I think most likely your problems are being caused from the way you have your user permissions set up in the remote database. This is not something that can be altered from the rails side.
Your database.yml file will just specify your login details into that database, but it's up to you to modify your user permissions settings in your database.
If you've got admin access into your database, I'd start by comparing the previous user's permissions to your own and seeing what's the difference.
All the best
I am getting an ActiveRecord PGError on Heroku not locally. The error message from the Heroku Logs is:
ActiveRecord::StatementInvalid (PGError: ERROR: column items.user_id does not exist
LINE 1: SELECT "items".* FROM "items" WHERE ("items".user_id = 4) OR
This code works locally (SQLite) but not on Heroku (postgresql) so I have been reading about the differences. At first I thought that it was a case issue as I recognised that it should be User_ID -- but now I'm not sure, it's something about "items" that isn't quite right and I can't work out exactly what it is.
As I said this works absolutely fine on my local machine, it's only on heroku and postgres that the problem comes up.
Thanks in advance.
It appears that your database does not have the 'user_id' column in the items table. This could be because your migration has not been run recently, or perhaps you have edited an existing migration rather than making a new migration to add this column.
Do you care about your data on heroku at the moment? If not you can use Taps to take whatever you have in your local database and set Heroku's database to a (virtual) synchronized copy of it. On your local machine from your project's directory:
heroku db:push
This will wipe your heroku database so use this wisely.
Try restarting the app, using the following command:
heroku restart
It was a case issue after all - postgres does (I believe) a downcase on everything before sending it to the db -- my user_id was actually called, inexplicably, User_ID; after migration everything is working -- thanks to those that helped.