"ERROR: must be member of role" When creating schema in PostgreSQL

PostgresqlRoles

Postgresql Problem Overview


I'm logged in with a superuser account and this is the process I'm doing:

1-> CREATE ROLE test WITH IN ROLE testroles PASSWORD 'testpasswd'
2-> CREATE SCHEMA AUTHORIZATION test

The role is correctly created but I'm getting this error when trying to create the Schema:

ERROR:  must be member of role "test"

Postgresql Solutions


Solution 1 - Postgresql

I ran into this issue when using CREATE DATABASE on Amazon RDS. I think it's essentially the same as using CREATE SCHEMA.

When using Amazon RDS, the user issuing the CREATE DATABASE must be a member of the role that will be the owner of the database. In my case, the superuser account I'm using is called root, and I'm going to create a role o which is going to own a database d:

postgres=> CREATE ROLE o;
CREATE ROLE

postgres=> CREATE DATABASE d OWNER = o;
ERROR:  must be member of role "o"

postgres=> GRANT o TO root;
GRANT ROLE

postgres=> CREATE DATABASE d OWNER = o;
CREATE DATABASE

Solution 2 - Postgresql

I came across this same problem, it is complaining about the current(master) user you are logged in with is not a member of the user group you are trying to create the schema for. You should grant the role access to your master user and it will let you create the SCHEMA without error:

GRANT <role> TO <master_user>;

Solution 3 - Postgresql

Are you using RDS? Because I get the same issue when I log in as the "superuser" that they create for you. The way that I was able to fix this was to create a new group role that included my super user and the user who owned the schema. So for you this would mean adding your super user and test user to a new roles group:

CREATE ROLE users NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
GRANT megausers TO testroles;
GRANT test TO testroles;

Now you should be able to create your schmea

Solution 4 - Postgresql

Had this problem with RDS too. To solve it:

Login as superuser

psql --host=xxxxxxx.rds.amazonaws.com --port=5432 --username=RDS_SUPERUSER_NAME --password --dbname=postgres

Create the User

CREATE USER newuser WITH CREATEDB PASSWORD 'password';

Logout

\q

Login as newuser

psql --host=xxxxxxx.rds.amazonaws.com --port=5432 --username=newuser --password --dbname=postgres

Create your DB/Schema

CREATE SCHEMA/DATABASE ....

Solution 5 - Postgresql

Don't we just have to grant the admin user membership to the service role?

create role service_role with password 'some_password';
create database service_db with owner service_role;
ERROR:  must be member of role "service_role"
grant admin_user service_role;
GRANT ROLE
create database service_db with owner service_role;
CREATE DATABASE

Solution 6 - Postgresql

I have encountered this issue on RDS as well. I logged in as the root user, created a role named myappuser and then tried creating a schema called superduper whose owner is myappuser.

I found a solution which works. Create the myappuser role, and make sure that this role at least has the permission to create databases (the privilege is called CREATEDB). After creating the myappuser role, I logged into the database as myappuser and created the superduper schema whose user is myappuser. This worked without any problems.

Solution 7 - Postgresql

I fixed it by logging in with postgres user (and then apply my changes, which was changing ownership in my case):

sudo -u postgres psql
ALTER DATABASE my_database OWNER TO new_user;

Solution 8 - Postgresql

I just ran into this using Amazon RDS.

A lot of the answers are already correct, but you can also just alter the role.

Here was my implementation

psql -h ${PGHOST} -p ${PGPORT:-5432} -U ${PGUSER:-postgres} -c "ALTER USER renderer WITH CREATEDB PASSWORD '$RENDERPASSWORD'"

So while changing the password, I am also adding the CREATEDB role permission to the role.

Solution 9 - Postgresql

I was facing the same issue. To resolve this, I logged in with the newly created role and created the database. Somehow, grant does not work in RDS Postgres.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUltranukeView Question on Stackoverflow
Solution 1 - PostgresqlDavid JonesView Answer on Stackoverflow
Solution 2 - PostgresqlJames ChingView Answer on Stackoverflow
Solution 3 - PostgresqlbionicseraphView Answer on Stackoverflow
Solution 4 - PostgresqlRafael OliveiraView Answer on Stackoverflow
Solution 5 - PostgresqljorfusView Answer on Stackoverflow
Solution 6 - PostgresqlKrystian CybulskiView Answer on Stackoverflow
Solution 7 - PostgresqlDevonDahonView Answer on Stackoverflow
Solution 8 - PostgresqlsprutView Answer on Stackoverflow
Solution 9 - PostgresqlSidView Answer on Stackoverflow