
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create a Cloud SQL for PostgreSQL instance with CMEK enabled
/ 40
Enable and configure pgAudit on a Cloud SQL for PostgreSQL database
/ 30
Configure Cloud SQL IAM database authentication
/ 30
Customer-managed encryption keys (CMEK) let you use your own cryptographic keys for data at rest in Cloud SQL. After adding customer-managed encryption keys, whenever an API call is made, Cloud SQL uses your key to access data.
This lab provides you with step-by-step guidance on how to secure a Cloud SQL for PostgreSQL instance. You first deploy a new Cloud SQL instance using a CMEK. Once you have created the Cloud SQL for PostgreSQL instance, you configure pgAudit to selectively record and track SQL operations performed against that instance, and finally you configure and test Cloud SQL IAM database authentication.
The content of this hands-on lab will be most applicable to PostgreSQL DBAs. This lab is designed to give professionals hands-on experience setting up and configuring Google Cloud resources to support PostgreSQL.
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
In this task, you create a Cloud SQL for PostgreSQL instance with CMEK enabled. It is imperative that you keep the keys safe as you cannot manage your database without them.
You can create the service account you require for Cloud SQL CMEK using the gcloud beta services identity create
command.
This creates the service account that you will bind to the CMEK in a later step.
In this section, you create a Cloud KMS keyring and key to use with CMEK.
The service account name is the same name that was returned by the gcloud beta services identity create
command in the previous sub-task.
In this section, you create a Cloud SQL for PostgreSQL instance with CMEK enabled. It is not possible to patch an existing instance to enable CMEK, so you should bear this in mind if you plan to use CMEK to encrypt your data.
In order to access your Cloud SQL instance from external development or application environments, you can configure the Cloud SQL instance with a public IP address and control access by allowlisting the IP address of those environments. This limits access to the public interface to those address ranges that you specify.
You treat the Compute Engine VM instance in the lab as a development environment and therefore need the to allow list the external IP address of that instance. You also add the external IP address of the Cloud Shell to the allowlist to make it easier to complete tasks later in the lab.
bastion-vm
VM instance:In this task, you enable and configure the pgAudit database extension which enables fine-grained control of logging of all types of database activity.
Patching Cloud SQL instance...done
, you can proceed to the next step.
In Cloud Console, on the Navigation menu (), click SQL.
Click on the Cloud SQL instance named postgres-orders
.
In the Cloud SQL Overview panel, top menu, click Restart to restart the instance after the patch that you ran in step 1.
If prompted again, click Restart again in the pop-up dialog.
Restarted postgres-orders
), you can proceed to the next step.
A command to connect to the instance will auto-populate in Cloud Shell.
supersecret!
when prompted.A psql session will start in Cloud Shell.
orders
database and enable the pgAudit extension to log all reads and writes:Enter the password supersecret!
again.
In psql, run the following command to create and configure the database extension:
In this section, you enable Audit Logging in Cloud Console.
you don't have permission to view inherited audit logs configuration data for one or more parent resources
, you can safely ignore the message and continue to the next step.
In the Filter box under Data access audit logs configuration, type Cloud SQL
, and select the entry in the drop-down list.
Enable the checkbox for Cloud SQL
on the left, and then enable the following checkboxes in the Info Panel on the right:
In this section, you populate the orders
database with data provided to you.
Click the + icon on the Cloud Shell title bar to open a new tab in the Cloud Shell.
In the new tab, run the following to download the data and database population scripts:
SELECT
operations on a particular relation (such as the order_items
table):SELECT
query below :The output is 500 rows long, so you can enter q
to close the results and return to the orders=>
prompt.
Repeat the steps 5-6 for the other two query tabs in the code block.
Run the following to exit psql:
In this step you will view the logging of your database updates and queries in the pgAudit logs.
In Cloud Console, on the Navigation menu (), click View all products. Under Observablity, click Logging to open the Logs Explorer page.
In the Query tab of the Logs Explorer, paste the following, and click Run query:
SELECT
queries you ran earlier.SELECT
queries you ran.In the Query results panel below the histogram, the log entries are listed.
protoPayload.request
you will see the SELECT
query.In this task, you configure Cloud SQL IAM database authentication. All of the database access and update tasks you have performed so far have used built-in PostgreSQL user accounts. You can also create Cloud SQL for PostgreSQL users using Cloud IAM accounts. Database users can authenticate to Cloud SQL using Cloud IAM instead of using built-in database accounts and fine-grained permissions at the database level can be granted to those users.
In this task, you configure the lab user account as a Cloud SQL IAM user, grant that user access to the orders.order_items
database table using the postgres administrator account, and then test access to the orders.order_items
database table from the command line using the psql command line utility.
The authentication process that is used in this task is explained in detail in the IAM authentication documentation for Cloud SQL for PostgreSQL.
You attempt to access the database using a Cloud IAM user before Cloud SQL IAM authentication has been enabled in order to establish that the Cloud IAM user cannot initially access the data. You will see this connection attempt fail before you proceed to the next section to address the issue.
orders
database using the lab student account as the username:This connection attempt fails, and you see an authentication failed message similar to the following because the Cloud SQL IAM user has not been created yet:
Cloud SQL IAM database authentication uses OAuth 2.0 access tokens is the Cloud IAM user password, which are short-lived and only valid for one hour so you should regenerate the token every time you need to authenticate. The access token should always be passed into the psql command using the PGPASSWORD environment variable as the buffer for the psql password parameter is too small to hold the OAuth 2.0 token string.
In this section, you create a Cloud SQL IAM user and confirm that Cloud SQL IAM user authentication has been enabled.
In Cloud Console, on the Navigation menu (), click SQL
Click on the Cloud SQL instance named postgres-orders
.
In the Configuration panel on the right, note that the Database flags and parameters list includes pgAudit.log and cloudsql.enable_pgaudit only.
In the SQL menu (left panel) under Primary instance, click Users to open the Users panel.
Click Add user account.
Select Cloud IAM.
In the Principal box enter the lab student name:
Click Add.
Wait for the new user to be successfully added.
On the main overview page for instance, in the Configuration panel on the right, note cloudsql.iam_authentication has been added to the Database flags and parameters list.
You now connect to the postgres-orders
instance using the built in postgres
administrator account and grant access to the orders.order_items
table to the Cloud IAM user.
A command to connect to the instance will auto-populate in Cloud Shell.
Run that command as is, and enter the password supersecret!
when prompted.
Enter the following SQL command to switch to the orders
database:
When prompted for a password enter supersecret!
again.
order_items
table. The Cloud IAM username for the lab has been inserted into this query for you.You now repeat the attempt to access the database using a Cloud IAM user after Cloud SQL IAM authentication has been enabled in order to establish that the Cloud IAM user can now access the data.
You can now test access to the database again using the Cloud IAM user instead of the built-in postgres
user:
This connection succeeds, and you are now connected to the instance using Cloud IAM user authentication.
This now returns a successful result:
This query does not return a successful result:
In this lab, you secured data at rest in Cloud SQL for PostgreSQL using a customer managed encryption key, enabled pgAudit, and configured Cloud SQL IAM database user authentication.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated July 2, 2025
Lab Last Tested July 2, 2025
Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one