My application is a multi-tenant one. I am creating a single database per tenant/user. So far, I was creating all the databases with the following command from c# code.
CREATE DATABASE @userDBName WITH OWNER = @dbOwner ENCODING = 'UTF8';
This way I see that all databases have got the same owner, and that owner has got access to all tables within all databases.
Now, according to the new security requirement, what we want to implement is that every database should be accessible by only a single user, and user of one database should not have access to the other database.
As, I am dealing with the concepts of roles/users in Postgres for the first time, I am unable to find luck in whatever I have done so far.
I created users, and tried granting them access on a single database
CREATE USER test WITH ENCRYPTED PASSWORD '123456';
GRANT ALL PRIVILEGES ON DATABASE userdb1 TO test;
Now, when in my connection string in c# I give the user Id='test' and Password as above, I get permission denied error.
Also, from PgAdmin, when I go to the database, and try to change the owner to the new user I have created above, I get this error
ERROR: must be member of role test
What exactly needs to be done? I have explored all possible solutions on the internet, and tried almost everything, but nothing is helping out much.