users and roles

created onApril 24, 2017
last modified onJune 24, 2022

working with users

creating a user

the bash command createuser can be used to create a PostgreSQL user. To create a user that is allowed to create databases, with username bob:

postgres@idoru:/$ createuser -d -P bob

  • -d: allow user to create databases
  • -P prompt for pasword

setting password of a user

to change the password of user bob to ‘n3w-s3cr3t-p4w0rd’:

postgres=# alter user bob with password 'n3w-s3cr3t-p4w0rd'; ALTER ROLE

working with roles

creating a role

postgres=# CREATE ROLE whatever WITH LOGIN ENCRYPTED PASSWORD 'guessme';

deleting a role

postgres=# DROP ROLE whatever;</code>

listing roles

postgres=# \du

sample output

postgres=# \du List of roles Role name | Attributes | Member of ----------+------------------------------------------------------------+----------- gitea | Create DB | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} rudolf | Create DB | {}