
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 restart it, you'll have to start 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 will 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 step you will 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 step you will 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 will treat the Compute Engine VM instance in the lab as a development environment and will therefore need the to allow list the external IP address of that instance. You will 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 will enable and configure the pgAudit database extension which enables fine-grained control of logging of all types of database activity.
Hit enter to confirm and continue. Wait a minute for the patch command to complete before continuing.
In Cloud Console, on the Navigation menu (), click Databases > SQL and click on the Cloud SQL instance named
postgres-orders
.
In the Cloud SQL Overview panel, top menu, click Restart and Restart again in the pop-up dialog.
It will take a minute to restart your Cloud SQL for PostgreSQL instance, then continue below.
In Cloud Console, in the Connect to this instance section, click Open Cloud Shell. A command will be auto-populated to the Cloud Shell.
Run that command and enter the password supersecret!
when prompted. A psql session will start in Cloud Shell.
In psql, to create the orders
database and enable the pgAudit extension to log all reads and writes, run:
Enter the password supersecret!
again.
In psql, to create and configure the database extension, run:
In this step you will enable Audit Logging in Cloud Console.
In Cloud Console, on the Navigation menu (), click IAM & Admin > Audit Logs.
In the Filter box, type Cloud SQL
and select the entry in the drop-down list.
Check the checkbox for Cloud SQL
on the left and all the checkboxes in the Info Panel on the right.
Click the Save button in the Info Panel.
In this step you will populate the orders
database with data provided to you.
It will take a minute for the orders
database to be populated.
SELECT
operations on a particular relation, for example the order_items
table, run the following commands:SELECT
query below :The output is 500 rows long, so hit q
and the colon prompt to return to the orders=>
prompt.
Repeat the last two steps for the other two queries tabs in the code block.
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 Observablity > Logging > Logs Explorer.
In the Query tab of the Logs Explorer, paste the following and click the Run query button:
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 will 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 will 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 will attempt to access the database using a Cloud IAM user before Cloud SQL IAM authentication haas been enabled in order to establish that the Cloud IAM user cannot initially access the data.
orders
database using the lab student account as the username:This connection attempt will fail and you will 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.
You will now 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 Databases > SQL and click on the Cloud SQL instance named
postgres-orders
.
In the Cloud SQL Overview panel, in the Configuration panel note that the Database flags list includes pgAudit.log and cloudsql.enable_pgaudit only.
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 added.
Click Overview and now note that cloudsql.iam_authentication has been added to the list of database flags.
You will 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.
postgres-orders
Cloud SQL instance as the postgres
administrative user:When prompted enter the password: supersecret!
.
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 will 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 the instance using Cloud IAM user authentication.
This will now return a successful result:
This will now 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 April 23, 2024
Lab Last Tested April 15, 2024
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