Configure permissions in Azure Database for PostgreSQL
In this lab exercises, you’ll assign RBAC roles to control access to Azure Database for PostgreSQL resources and PostgreSQL GRANTS to control access to database operations.
Before you start
You need your own Azure subscription to complete this exercise. If you do not have an Azure subscription, create an Azure free trial.
To complete these exercises, you need to install a PostgreSQL server that is connected to Microsoft Entra ID - formerly Azure Active Directory
Create a Resource Group
- In a web browser, navigate to the Azure portal. Sign in using an Owner or Contributor account.
- Under Azure services, select Resource Groups, then select + Create.
- Check that the correct subscription is displayed, then enter the Resource group name as rg-PostgreSQL_Entra. Select a Region.
- Select Review + create. Then select Create.
Create an Azure Database for PostgreSQL flexible server
- Under Azure services, select + Create a resource.
- In Search the Marketplace, type
azure database for postgresql flexible server
, choose Azure Database for PostgreSQL Flexible Server and click Create.
- In Search the Marketplace, type
- On the Flexible server Basics tab, enter each field as follows:
- Subscription - your subscription.
- Resource group - rg-PostgreSQL_Entra.
- Server name - psql-postgresql-fx7777 (Server name must be globally unique, so replace 7777 with four random numbers).
- Region - select the same region as the resource group.
- PostgreSQL version - select 16.
- Workload type - Development.
- Compute + storage - Burstable, B1ms.
- Availability zone - No preference.
- High availability - leave unchecked.
- Authentication Method - choose PostgreSQL and Microsoft Entra authentication
- Set Microsoft Entra admin - select Set admin
- Search for your account in Select Microsoft Entra Admins and (o) your account and click Select
- In admin username, enter
demo
. - In password, enter a suitably complex password.
- Select Next: Networking >.
- On the Flexible server Networking tab, enter each field as follows:
- Connectivity method: (o) Public access (allowed IP addresses) and Private endpoint
- Public access, select Allow public access to this resource through the internet using a public IP address
- Under Firewall rules, select + Add current client IP address, to add your current IP address as a firewall rule. You can optionally name this firewall rule to something meaningful. Also add Add 0.0.0.0 - 255.255.255.255 and click Continue
- Select Review + create. Review your settings, then select Create to create your Azure Database for PostgreSQL Flexible server. When the deployment is complete, select Go to resource ready for the next step.
Install Azure Data Studio
To install Azure Data Studio for use with Azure Database for PostgreSQL:
- In a browser, navigate to Download and install Azure Data Studio and under the Windows platform, select User installer (recommended). The executable file is downloaded to your Downloads folder.
- Select Open file.
- The License agreement is displayed. Read and accept the agreement, then select Next.
- In Select additional Tasks, select Add to PATH, and any other additions you require. Select Next.
- The Ready to Install dialog box is displayed. Review your settings. Select Back to make changes or select Install.
- The Completing the Azure Data Studio Setup Wizard dialog box is displayed. Select Finish. Azure Data Studio starts.
Install the PostgreSQL extension
- Open Azure Data Studio if it is not already open.
- From the left menu, select Extensions to display the Extensions panel.
- In the search bar, enter PostgreSQL. The PostgreSQL extension for Azure Data Studio icon is displayed.
- Select Install. The extension installs.
Connect to Azure Database for PostgreSQL flexible server
- Open Azure Data Studio if it is not already open.
- From the left menu, select Connections.
- Select New Connection.
- Under Connection Details, in Connection type select PostgreSQL from the drop-down list.
- In Server name, enter the full server name as it appears on the Azure portal.
- In Authentication type, leave Password.
- In User name and Password, enter the user name demo and the complex password you created above
- Select [ x ] Remember password.
- The remaining fields are optional.
- Select Connect. You are connected to the Azure Database for PostgreSQL server.
- A list of the server databases is displayed. This includes system databases, and user databases.
Create the zoo database
- Either navigate to the folder with your exercise script files, or download the Lab2_ZooDb.sql from MSLearn PostgreSQL Labs.
- Open Azure Data Studio if it is not already open.
- Select File, Open file and navigate to the folder where you saved the script. Select ../Allfiles/Labs/02/Lab2_ZooDb.sql and Open. If a trust warning is displayed select Open.
- Run the script. The zoodb database is created.
Create a new user account in Microsoft Entra ID
[!NOTE] In most production or development environments, it is very possible you won’t have the subscription account privileges to create accounts on your Microsoft Entra ID service. In that case, if allowed by your organization, try asking your Microsoft Entra ID administrator to create a test account for you. If you are unable to get the test Entra account, skip this section and continue to the GRANT access to Azure Database for PostgreSQL section.
- In the Azure portal, sign in using an Owner account and navigate to Microsoft Entra ID.
- Under Manage, select Users.
- At the top-left, select New user and then select Create new user.
- In the New user page, enter these details and then select Create:
- User principal name: Choose a Principle name
- Display Name: Choose a Display Name
- Password: Untick Auto-generate password and then enter a strong password. Take note of the principal name and password.
- Click Review + create
[!TIP] When the user is created, make a note of the full User principal name so that you can use it later to log in.
Assign the Reader role
- In the Azure portal, select All resources and then select your Azure Database for PostgreSQL resource.
- Select Access control (IAM) and then select Role assignments. The new account doesn’t appear in the list.
- Select + Add and then select Add role assignment.
- Select the Reader role, and then select Next.
- Choose + Select members, add the new account you added in the previous step to the list of members and then select Next.
- Select Review + Assign.
Test the Reader role
- In the top-right of the Azure portal, select your user account and then select Sign out.
- Sign in as the new user, with the user principal name and the password that you noted. Replace the default password if you’re prompted to and make a note of the new one.
- Choose Ask me later if prompted for Multi Factor Authentication
- In the portal home page, select All resources and then select your Azure Database for PostgreSQL resource.
- Select Stop. An error is displayed, because the Reader role enables you to see the resource but not change it.
Assign the Contributor role
- In the top-right of the Azure portal, select the new account’s user account and then select Sign out.
- Sign in using your original Owner account.
- Navigate to your Azure Database for PostgreSQL resource, and then select Access Control (IAM).
- Select + Add and then select Add role assignment.
- Choose Privileged administrator roles
- Select the Contributor role, and then select Next.
- Add the new account you previously added to the list of members and then select Next.
- Select Review + Assign.
- Select Role Assignments. The new account now has assignments for both Reader and Contributor roles.
Test the Contributor role
- In the top-right of the Azure portal, select your user account and then select Sign out.
- Sign in as the new account, with the user principal name and password that you noted.
- In the portal home page, select All resources and then select your Azure Database for MySQL resource.
- Select Stop and then select Yes. This time, the server stops without errors because the new account has the necessary role assigned.
- Select Start to ensure that the PostgreSQL server is ready for the next steps.
- In the top-right of the Azure portal, select the new account’s user account and then select Sign out.
- Sign in using your original Owner account.
GRANT access to Azure Database for PostgreSQL
- Open Azure Data Studio and connect to your Azure Database for PostgreSQL server using the demo user that you set as the administrator above.
-
In the query pane, execute this code against postgres database. Twelve user roles should be returned, including the demo role that you’re using to connect:
SELECT rolname FROM pg_catalog.pg_roles;
-
To create a new role, execute this code
CREATE ROLE dbuser WITH LOGIN NOSUPERUSER INHERIT CREATEDB NOCREATEROLE NOREPLICATION PASSWORD 'R3placeWithAComplexPW!'; GRANT CONNECT ON DATABASE zoodb TO dbuser;
[!NOTE] Make sure to replace the password in the script above for a complex password.
- To list the new role, execute the above SELECT query in pg_catalog.pg_roles again. You should see the dbuser role listed.
-
To enable the new role to query and modify data in the animal table in the zoodb database, execute this code against the zoodb database:
GRANT SELECT, INSERT, UPDATE, DELETE ON animal TO dbuser;
Test the new role
- In Azure Data Studio, in the list of CONNECTIONS select the new connection button.
- In the Connection type list, select PostgreSQL.
- In the Server name textbox, type the fully qualified server name for your Azure Database for PostgreSQL resource. You can copy it from the Azure portal.
- In the Authentication type list, select Password.
- In the Username textbox, type dbuser and in the Password textbox type the complex password you created the account with.
- Select the Remember password checkbox and then select Connect.
-
Select New query and then execute this code:
SELECT * FROM animal;
-
To test whether you have the UPDATE privilege, execute this code:
UPDATE animal SET name = 'Linda Lioness' WHERE ani_id = 7; SELECT * FROM animal;
-
To test whether you have the DROP privilege, execute this code. If there’s an error, examine the error code:
DROP TABLE animal;
-
To test whether you have the GRANT privilege, execute this code:
GRANT ALL PRIVILEGES ON animal TO dbuser;
These tests demonstrate that the new user can execute Data Manipulation Language (DML) commands to query and modify data but can’t use Data Definition Language (DDL) commands to change the schema. Also, the new user can’t GRANT any new privileges to circumvent the permissions.
Clean-Up
You will not use this PostgreSQL server again so please delete the resource group you created which will remove the server.