54

I have the following postgres users which I can view by invoking the \du command in the terminal as follows:

postgres=# \du 

                            List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 tutorial1 |                                                | {}

According to the postgres documentation, I should be able to drop the user called "tutorial1" by entering the following command:

postgres=# DROP USER tutorial1

However, when I use that command nothing happens. The documentation doesn't provide any hints as to why this isn't working, nor does it provide clear examples.

That being said-- what is the command to drop this user?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
psvj
  • 8,280
  • 8
  • 30
  • 44

6 Answers6

69

I've wasted way too much time on trying to find all the things to unwind. In addition to the above (or possibly as a complete replacement), refer to this answer to a similar question: https://stackoverflow.com/a/11750309/647581

DROP OWNED BY your_user;
DROP USER your_user;

did the trick for me

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Patrick Herrera
  • 3,120
  • 26
  • 21
  • This will drop (delete) all of the objects that your_user owns, right? We should probably edit this answer to include a big warning about that? – KayakinKoder Nov 21 '22 at 23:36
36

If you find yourself here (like me) because you are unable to drop the user, the following template may be helpful:

REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM tutorial1;
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public FROM tutorial1;
REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public FROM tutorial1;
DROP USER tutorial1;

The user may have privileges in other schemas, in which case you will have to run the appropriate REVOKE line with "public" replaced by the correct schema. To show all of the schemas and privilege types for a user, I edited the \dp command to make this query:

SELECT 
  n.nspname as "Schema",
  CASE c.relkind 
    WHEN 'r' THEN 'table' 
    WHEN 'v' THEN 'view' 
    WHEN 'm' THEN 'materialized view' 
    WHEN 'S' THEN 'sequence' 
    WHEN 'f' THEN 'foreign table' 
  END as "Type"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE pg_catalog.array_to_string(c.relacl, E'\n') LIKE '%postgres%';

I'm not sure which privilege types correspond to revoking on TABLES, SEQUENCES, or FUNCTIONS, but I think all of them fall under one of the three.

Sasha Kondrashov
  • 4,038
  • 1
  • 18
  • 29
25

Your command is ok but you forget to put ; at the end of the command.

Try like this

postgres=# DROP USER tutorial1; (Note I put semicolon at the end)
Shubho Shaha
  • 1,869
  • 1
  • 16
  • 22
  • 1
    Downvoted your answer because of this error `ERROR: role "tutorial1" cannot be dropped because some objects depend on it` – kabrice Feb 24 '18 at 20:56
  • 9
    @kabrice: that downvote is wrong. The fact that objects depend on the role has nothing to do with the question on how to run the statement to drop a role –  Mar 12 '18 at 08:53
22

First in Ubuntu terminal actions:

$ sudo -su postgres
$ psql postgres

then in postgres' terminal:

# \du
# drop user 'user_name';
# \du

[NOTE]:

Don't forget the semicolon ";"


[UPDATE]:

If your Postgres is located in a docker container ...

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
5

The answer from Timofey almost worked for me, but I also needed to revoke permissions at schema level. So my drop script looked like this:

REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM tutorial1;
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public FROM tutorial1;
REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public FROM tutorial1;
REVOKE ALL PRIVILEGES ON SCHEMA public FROM tutorial1;
DROP USER tutorial1;

So revoking may need to happen at any of the levels that happen here: https://www.postgresql.org/docs/9.1/static/sql-revoke.html

EDIT:

While attempting this again I got the error

ERROR: role cannot be dropped because some objects depend on it

So I had to add the code from this answer to my script https://stackoverflow.com/a/51257346/9540123

DatabaseShouter
  • 814
  • 9
  • 11
4

In case you have dot in user name test.user

drop user test.user;

ERROR:  42601: syntax error at or near "."

drop user "test.user";

Should solve it.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103