This time we will learn:
- Changing role password
- Creating a role using postgres utility and psql
- Adding attributes to a role during creation
- Connect to a cluster as a role
- Deleting a role using postgress utility and psql
- Connect to a cluster as a role
Please make sure the postgres server is running. Check this Getting Started with PostgreSQL on Mac Part 1.
Changing the postgres default password
It’s a best practice to change the postgres password for security reason.
Enter this to open postgres shell:
BASH1psql postgresEnter this in postgres shell to change the default password (no passowrd), the it will prompt to enter a new password:
BASH1\password
Creating a role
By default after installation, a default postgres user or role with your Mac username will be created as a superuser. Creating a new role instead of using the default superuser role is always a best practice.
If you want to add more there are two ways to create a role (user):
Through the PostgreSQL utilities (installed with postgres installation) in the terminal.
Using client utilities
createuser.BASH1createuser <username>Directly in database or through psql shell of a superuser.
BASH1CREATE ROLE <username> WITH LOGIN PASSWORD 'your password';
1. Create a role using utilities
This newly created user will have a default attributes which is empty. Okay, now let’s create a role using the postgres utilities.
Open terminal and type this command:
BASH1createuser testuserThis will create a new role with name
testuser. This role will does not have a databaseNow, let’s connect to db as this role. Run this on terminal to connect to the default db:
BASH1psql postgres -U testuserAs you can see from the terminal log, we are logged in as a non superuser. (
>).BASH1# terminal log 2psql (12.4) 3Type "help" for help. 4 5postgres=>Let’s check the all roles that exist in the database. Run this command on postgres shell:
BASH1 \duAs you can see, there our newly created role exists already but with empty attributes (default).
BASH1Role name | Attributes | Member of 2 3-----------+------------------------------------------------------------+----------- 4macuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {} 5testuser |We can add more attributes during role creation by providing the attributes or create the role interactively. For example, creating the role interactively:
BASH1createuser testuser1 --interactive 2# prompt 3Shall the new role be a superuser? (y/n) n 4Shall the new role be allowed to create databases? (y/n) y 5Shall the new role be allowed to create more new roles? (y/n) nNow if we go into psql shell again and check the roles, we will see
testuser1will have an attribute we have specified during interactive creation:BASH1postgres=> \du 2 List of roles 3 4Role name | Attributes | Member of 5-----------+------------------------------------------------------------+----------- 6macuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {} 7testuser | | {} 8testuser1 | Create DBLet’s delete all the roles we have created. Run these commands on terminal (assuming they don’t have any databases yet):
BASH1dropuser testuser 2dropuser testuser1
2. Creating a role using psql shell
Open terminal and enter this to open psql shell as a superuser:
BASH1psql postgresType and enter this sql query in psql shell to create a role. Password can be
NULL, but it’s not recommended:BASH1CREATE ROLE testuser WITH LOGIN PASSWORD 'yourpassword';Check if the user is created by listing all the users in cluster:
BASH1\du 2 3# output 4 List of roles 5Role name | Attributes | Member of 6-----------+------------------------------------------------------------+----------- 7macuser | Superuser, Create role, Create DB, Replication, Bypass RLS | {} 8testuser | | {} 9 10postgres=#Now, let’s add some attributes. Type and enter this command:
BASH1ALTER ROLE testuser CREATEDB;Now if check the list of users again using
\du:BASH1 List of roles 2 3 Role name | Attributes | Member of 4 -----------+------------------------------------------------------------+----------- 5 swiftmage | Superuser, Create role, Create DB, Replication, Bypass RLS | {} 6 testuser | Create DBLet’s delete this new role we have created:
BASH1DROP ROLE testuser;Check it’s gone from the list using
\du.Type and enter
\qto quit psql shell.
