Rails: InsufficientPrivilege: ERROR: permission denied for relation schema_migrations - ruby-on-rails

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.

Related

PostgreSQL load existing project's db

i'm setting up a RoR project and i've got a database.yml file with a specific database with user and password. What i did was to add this file into my project and want to load it. when i run rails s i get an authentication error for the db user. Should i create a new user? should i create the db? Thing is i want to load the records that already exists on it, that's why i'm a little bit lost.
Important fact: i already have a user created with a specific password (for another db) and the username for the new db is the same as my old user (with different password). Would that be a problem?
A database user can have only one password. Change the password in your database.yml to the password of your existing database user.
You have to create the database user that's specified in the database.yml file.
For Postgres, you have to use createuser for this. You'll also have to set the corresponding password and then add the proper privileges.
This might help: https://www.digitalocean.com/community/tutorials/how-to-use-roles-and-manage-grant-permissions-in-postgresql-on-a-vps--2.
Then you can run bin/rails s. The records won't be deleted.

Rails migrations & PostgreSQL: separate user for migration and setting privileges for production user

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.

Rails DB Migration - permissions

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

PGError:Error: must be owner of relation

I have using rails application with PostgreSql database.
My application is working fine with PostgreSql.
I want to run my migration on server, I run a command RAILS_ENV=production rake db:migrate
then I get following error:
PGError:Error: must be owner of
relation table_name
I don't understand why this error occurs?
Please suggest any solution to resolve this error.
Thanks!
You want to change something in the table, but you don't have the permissions to do so. Only the owner of the table can do so.
Use a different database role, the owners role, and you're fine.

Error restoring postgres database backup

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?

Resources