Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deactivate postgres user account

Tags:

sql

postgresql

Is there a way to deactivate a postgres user account via an SQL statement?

I want to prevent a user from using their database, but without deleting the user or their databases.

like image 465
Kai Avatar asked Nov 28 '10 16:11

Kai


People also ask

How do I disconnect from PostgreSQL?

The meta-command for exiting psql is \q .

How do you delete a user on pgAdmin 4?

To discard a user, and revoke access to pgAdmin, click the trash icon to the left of the row and confirm deletion in the Delete user? dialog. If the user has created some shared servers, then the Change Ownership dialog will appear to change the ownership of a shared server.

Why do I have a postgres user?

PostgreSQL is database software and database-related programs may use it. Perhaps some database-related program you have installed uses it. These database programs often will add a user; QuickBooks does this for example. Only you know what programs you've installed.


2 Answers

You might also consider ALTER USER someone WITH NOLOGIN; which is probably the right solution if you have multiple databases in the same back-end and want to block the user entirely.

To lock an account:

# Lock an account
ALTER USER someone WITH NOLOGIN;

To unlock an account:

# Un-lock an account
ALTER USER someone WITH LOGIN;

Check account is locked or not:

select rolcanlogin from pg_roles where rolname='database_user';

OUTPUT:
rolcanlogin
-------------
 f
(1 row)

f = false # Can not login
t = true # Can login
like image 131
Andrew Avatar answered Sep 30 '22 12:09

Andrew


Take a look at the REVOKE command.

In order for a user to connect to a database he has been granted the CONNECT privilege. Just REVOKE this privilege and he won't be able to use this database.

like image 38
Spredzy Avatar answered Sep 30 '22 12:09

Spredzy