Manage Users and Databases
Create, inspect, rotate and delete the users and databases of a Managed PostgreSQL service with the exo CLI, and see how they relate to the roles you create in SQL.
Users and Roles
Every user of the service is a PostgreSQL role. The platform keeps a list of these roles and, for some of them, their password:
- A user created with
exo dbaas user createis a role withLOGINand a password generated and stored by the platform. You can read the password back and rotate it without connecting to the database. - A role created in SQL with
CREATE ROLEorCREATE USERalso appears inexo dbaas user listandexo dbaas show, as anormaluser, includingNOLOGINgroup roles. The platform holds no password for it until you runexo dbaas user reset-credentialson it.
Use platform-managed passwords for the identities your applications log in with, and SQL group
roles, such as readonly or app_readwrite, to bundle privileges.
The avnadmin User
Every service has avnadmin, its primary user. avnadmin is not a superuser. It has
CREATEDB, CREATEROLE, REPLICATION and BYPASSRLS, and it is a member, through the
platform role avnadmin_group, of pg_signal_backend, pg_read_all_stats,
pg_stat_scan_tables, pg_create_subscription and, on PostgreSQL 18,
pg_signal_autovacuum_worker. It is not a member of pg_monitor, pg_read_all_settings or
pg_read_all_data.
Check the attributes of the role you are connected as:
SELECT rolname, rolsuper, rolcreatedb, rolcreaterole, rolreplication, rolbypassrls
FROM pg_roles
WHERE rolname = current_user;List the roles it is a member of, directly or through other roles:
SELECT rolname AS granted_role
FROM pg_roles
WHERE pg_has_role(current_user, oid, 'MEMBER')
AND rolname <> current_user
ORDER BY 1;On a new PostgreSQL 18 service this returns nine roles. The list grows as you add users:
avnadmin holds ADMIN OPTION on every user created with exo dbaas user create, so it can
grant those roles and change their attributes and passwords. It is also a member of every role it
creates in SQL, with the privileges of that role, because createrole_self_grant is set to
inherit,set on the service.
avnadmin is the user of the connection URI that the platform generates. Do not delete it.
Because it is a member of pg_signal_backend, pg_cancel_backend() and
pg_terminate_backend() work for the sessions of non-superuser roles.
Create a User
exo dbaas user create SERVICE_NAME USERNAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service, for examplemy-postgresql.USERNAME: name of the user, 1 to 64 characters.ZONE: zone the service runs in, for examplech-gva-2.
The user gets LOGIN and a generated password, and no other attribute or role membership.
To create a user with the REPLICATION attribute, for example for logical replication, add
--pg-allow-replication:
exo dbaas user create SERVICE_NAME USERNAME -z ZONE --pg-allow-replicationThe CLI accepts this flag only at creation. To change it on an existing user, use the API:
echo '{"allow-replication":true}' | \
exo api update-dbaas-postgres-allow-replication SERVICE_NAME USERNAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.USERNAME: name of an existing user.ZONE: zone the service runs in.
Send false to remove the attribute.
Reveal and Reset Credentials
Read the stored password of a user:
exo dbaas user reveal-secrets SERVICE_NAME USERNAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.USERNAME: name of the user.ZONE: zone the service runs in.
Rotate the password. Without --password, a new one is generated:
exo dbaas user reset-credentials SERVICE_NAME USERNAME -z ZONETo choose the password, add --password PASSWORD, with 8 to 256 characters. The old password
stops working immediately: update the applications that use it.
Warning
Rotate the passwords of platform-managed users with reset-credentials, not with
ALTER ROLE ... PASSWORD in SQL. After a password change in SQL, the platform no longer holds
the password: reveal-secrets returns an empty password. Running reset-credentials again
puts the password back under platform control.
Read the Users and Databases
There is no API operation to list users or databases. Both are part of the service:
exo dbaas show SERVICE_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.ZONE: zone the service runs in.
The Users row lists each user with its type, primary for avnadmin and normal for the
others, and the Databases row lists the databases. exo dbaas user list prints the same users,
and exo dbaas user show SERVICE_NAME USERNAME -z ZONE adds whether replication is allowed.
exo dbaas database has no list command.
For scripts:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q 'databases'
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q 'users[].[username,type,"allow-replication"]'Caution
The full output of exo api get-dbaas-service-pg contains the password of every
platform-managed user. Project only the fields you need, as above, or treat the output as a
secret.
A role created in SQL can take a few seconds to appear in the list.
Create a Database
exo dbaas database create SERVICE_NAME DATABASE_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.DATABASE_NAME: name of the database, 1 to 40 characters.ZONE: zone the service runs in.
The database is owned by avnadmin, encoded in UTF8, with the en_US.UTF-8 collation and
character classification, like defaultdb. To choose the collation, add --pg-lc-collate, for
example --pg-lc-collate de_CH.UTF-8. Check the result with:
SELECT datname, pg_get_userbyid(datdba) AS owner, datcollate, datctype
FROM pg_database
ORDER BY 1;The _aiven database is used by the platform. Do not connect to it or change it.
Delete a Database
exo dbaas database delete SERVICE_NAME DATABASE_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.DATABASE_NAME: name of the database to delete.ZONE: zone the service runs in.
Add -f to skip the confirmation, for example in a script.
Caution
Deleting a database deletes its data. To recover it, restore a backup or a point in time into a new service. See Backups and Restore.
Delete a User
exo dbaas user delete SERVICE_NAME USERNAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.USERNAME: name of the user to delete.ZONE: zone the service runs in.
Add -f to skip the confirmation. The command also works for roles created in SQL.
The objects the user owns, in every database, are reassigned to avnadmin, and the privileges
granted to the user are removed. If an application must keep owning those objects, reassign them
first, in each database:
REASSIGN OWNED BY USERNAME TO NEW_OWNER;Parameters:
USERNAME: the user you are about to delete.NEW_OWNER: the role that takes over its objects.
Bundle Privileges in Group Roles
Create NOLOGIN group roles in SQL, grant privileges to them, and grant the group roles to the
users. Adding an application then takes one GRANT. Run the following as avnadmin, connected
to the database concerned:
CREATE ROLE app_readwrite NOLOGIN;
GRANT CONNECT ON DATABASE defaultdb TO app_readwrite;
GRANT USAGE ON SCHEMA public TO app_readwrite;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_readwrite;
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO app_readwrite;
ALTER DEFAULT PRIVILEGES FOR ROLE avnadmin IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_readwrite;
ALTER DEFAULT PRIVILEGES FOR ROLE avnadmin IN SCHEMA public
GRANT USAGE ON SEQUENCES TO app_readwrite;
GRANT app_readwrite TO myapp;Replace myapp with the user of your application. The read-only equivalent, and how default
privileges depend on the role that creates the tables, are covered in
Create Read-Only Users.