PostgreSQL users, roles, and ownership
PostgreSQL 19.0
Use a local PostgreSQL server on port 5432. Start as the administrator postgres and use a fresh practice database named mydb and a new login role named nyinyizin. Run the sections in order. The psql command starts from your OS terminal. SQL and the backslash commands go inside psql.
On this page
- Open psql as postgres
- Create a login role and set its password
- Grant and remove individual role attributes
- Read and reset the SUPERUSER attribute
- Create mydb and set its owner
- Connect as nyinyizin
- Make a table owned by the new role
- Reconnect as postgres to mydb
- Restore ownership and remove the login role
- Official references
Open psql as postgres
Run this in your terminal. Use the postgres login from setup, and enter its password if prompted. If your installation uses a different administrator role, use that role consistently, including when restoring ownership in the last section. Use a different pair of names throughout if mydb or nyinyizin is already in use.
psql -h localhost -p 5432 -U postgres -d postgres
Create a login role and set its password
Run these lines inside psql. CREATE USER creates a role with LOGIN enabled. The password prompt asks you to enter the password twice without displaying it. Check the new role with the last line.
CREATE USER nyinyizin;
\password nyinyizin
\du nyinyizin
CREATE USER … WITH PASSWORD- SQL also accepts a password in CREATE USER. Use \password for this terminal exercise so the password does not appear in the SQL you type.
Grant and remove individual role attributes
This optional example grants CREATEDB to create databases and CREATEROLE to create and manage eligible roles. Inspect the changes with \du, then remove both attributes before proceeding. Changing these attributes does not change the role’s LOGIN setting.
ALTER USER nyinyizin WITH CREATEDB CREATEROLE;
\du nyinyizin
ALTER USER nyinyizin WITH NOCREATEDB NOCREATEROLE;
Read and reset the SUPERUSER attribute
A superuser bypasses PostgreSQL permission checks except the right to log in. This optional example is for a disposable local server. In one transaction as postgres, grant the attribute, inspect it, then reset the elevated attributes before committing. The temporary grant stays inside that transaction.
BEGIN;
ALTER USER nyinyizin WITH SUPERUSER;
\du nyinyizin
ALTER USER nyinyizin WITH NOSUPERUSER NOCREATEROLE NOCREATEDB NOREPLICATION NOBYPASSRLS;
COMMIT;
\du nyinyizin
Create mydb and set its owner
Still connected as postgres, create the practice database and transfer the database object to nyinyizin. The role can own this database with its elevated attributes removed. Confirm the Owner column shows nyinyizin and leave psql with \q.
CREATE DATABASE mydb;
ALTER DATABASE mydb OWNER TO nyinyizin;
\l mydb
\q
ALTER DATABASE … OWNER TO- Transfers ownership of the database object. Tables within it have their own owners.
Connect as nyinyizin
Run this in the OS terminal, then enter the password you set for nyinyizin. The default prompt for this ordinary role is mydb=>.
psql -h localhost -p 5432 -U nyinyizin -d mydb
psql- Starts the PostgreSQL command-line client.
-h localhost- Connects to the server on this computer over TCP.
-p 5432- Uses TCP port 5432.
-U nyinyizin- Authenticates as the login role nyinyizin.
-d mydb- Connects directly to mydb.
Make a table owned by the new role
Inside mydb, verify the connection and create a small disposable table as nyinyizin. Its owner is the role that creates it. Use \dt to verify the owner, then exit with \q.
\conninfo
SELECT current_user, current_database();
CREATE TABLE public.role_demo (id integer PRIMARY KEY);
\dt public.role_demo
\q
Reconnect as postgres to mydb
Run this in the OS terminal before restoring ownership and removing the practice role.
psql -h localhost -p 5432 -U postgres -d mydb
Restore ownership and remove the login role
Run these lines inside mydb as postgres after the nyinyizin session is closed. The first ALTER DATABASE restores the database owner. REASSIGN OWNED then transfers objects still owned by the practice role, including role_demo. Verify the table ownership before dropping the role.
ALTER DATABASE mydb OWNER TO postgres;
REASSIGN OWNED BY nyinyizin TO postgres;
\dt public.role_demo
DROP USER nyinyizin;
\du nyinyizin
REASSIGN OWNED- Changes ownership of the old role’s objects in the current database and shared objects such as databases and tablespaces. In this exercise the source is the new practice role.
DROP USER- Removes the role. PostgreSQL refuses if database objects or privileges still depend on it. If this happens outside the exercise, inspect the reported dependencies before retrying.