PostgreSQL CREATE ROLE Statement
Summary: in this tutorial, you will learn about the PostgreSQL roles and how to use the PostgreSQL CREATE ROLE
statement to create new roles.
PostgreSQL uses the concept of roles to represent user accounts. It doesn’t use the concept of users like other database systems.
Typically, roles that can log in to the PostgreSQL server are called login roles. They are equivalent to user accounts in other database systems.
When roles contain other roles, they are referred to as group roles.
Note that PostgreSQL combined the users and groups into roles since version 8.1
Introduction to PostgreSQL CREATE ROLE statement
To create a new role in a PostgreSQL server, you use the CREATE ROLE
statement.
Here’s the basic syntax of the CREATE ROLE
statement:
In this syntax, you specify the name of the role that you want to create after the CREATE ROLE
keywords.
When you create a role, it is valid in all databases within the database server (or cluster).
For example, the following statement uses the CREATE ROLE
statement to create a new role called bob
:
To retrieve all roles in the current PostgreSQL server, you can query them from the pg_roles
system catalog as follows:
Output:
Notice that the roles whose names start with pg_
are system roles. The postgres
is a superuser role created by the PostgreSQL installer.
In psql
, you can use the \du
command to show all roles that you create including the postgres role in the current PostgreSQL server:
Output:
The output indicates that the role bob
cannot log in.
To allow the bob
to log in to the PostgreSQL server, you need to add the LOGIN
attribute to it.
Role attributes
The attributes of a role define privileges for that role, including login, superuser status, database creation, role creation, password management, and so on.
Here’s the syntax for creating a new role with attributes.
In this syntax, the WITH
keyword is optional. The option
can be one or more attributes like SUPERUSER
, CREATEDB
, CREATEROLE
, etc.
1) Create login roles
For example, the following statement creates a role called alice
that has the login privilege and an initial password:
Note that you place the password in single quotes ('
).
Here’s the new roles list:
Now, you can use the role alice
to log in to the PostgreSQL database server using the psql
client tool:
It will prompt you for a password. You need to enter the password that you entered in the CREATE ROLE
statement to log in to the PostgreSQL server.
2) Create superuser roles
The following statement creates a role called john
that has the superuser
attribute.
The superuser role has all permissions within the PostgreSQL server. Therefore, you should create the superuser role only when necessary.
Notice that only a superuser role can create another superuser role.
3) Create roles with database creation permission
If you want to create roles that have the database creation privilege, you can use the CREATEDB
attribute:
4) Create roles with a validity period
To set a date and time after which the role’s password is no longer valid, you use the VALID UNTIL
attribute:
For example, the following statement creates a dev_api
role with password valid until the end of 2049:
After one second tick in 2050, the password of dev_api
is no longer valid.
5) Create roles with connection limit
To specify the number of concurrent connections a role can make, you use the CONNECTION LIMIT
attribute:
The following creates a new role called api
that can make 1000 concurrent connections:
The following psql
command shows all the roles that we have created so far:
Summary
- PostgreSQL uses roles to represent user accounts. A role that can log in is equivalent to a user account in other database systems.
- Use the role attributes to specify the privileges of the roles such as
LOGIN
allows the role to log in,CREATEDB
allows the role to create a new database,SUPERUSER
allows the role to have all privileges.