Database

Amazon Aurora Serverless PostgreSQL: On-Demand Databases in Seconds

Amazon Aurora Serverless PostgreSQL: On-Demand Databases in Seconds

Amazon Aurora Serverless PostgreSQL offers a game-changing approach to database management. Instead of stressing over instance sizing, capacity planning, and paying for idle resources, Aurora Serverless automatically adapts to your application’s needs. You only pay for what you use, making it an ideal fit for variable workloads, dev/test environments, and applications with unpredictable traffic spikes. Imagine spinning up a fully operational PostgreSQL database in seconds without manually provisioning the underlying infrastructure.

In this tutorial, we’ll walk through creating, connecting to, and managing an Amazon Aurora Serverless PostgreSQL database. You’ll learn how to leverage its auto-scaling capabilities, cost-saving features, and management simplicity. By the end of this guide, you’ll be ready to deploy Aurora Serverless PostgreSQL for your own applications, optimizing costs and boosting operational efficiency.

Before you begin, ensure you have an active AWS account and the necessary permissions to provision RDS and VPC resources.

Prerequisites

  • An active AWS account.
  • IAM permissions to create and manage RDS (Amazon Relational Database Service) and VPC (Virtual Private Cloud) resources.
  • AWS CLI (Command Line Interface) installed and configured (optional, but recommended).
  • A PostgreSQL client (e.g., psql) installed on your system to connect to the database.
  • Basic familiarity with PostgreSQL and relational database concepts.

Creating an Aurora Serverless PostgreSQL Database

Creating an Aurora Serverless PostgreSQL database is a straightforward process. We’ll use the AWS Management Console for this walkthrough, though you can easily automate it using the AWS CLI or Terraform.

Access the RDS Console

The first step is to navigate to the AWS RDS console.

  1. Log in to the AWS Management Console.
  2. Search for “RDS” in the search bar and select “RDS” (Relational Database Service).

Create a New Database

Once in the RDS console, you can start the database creation process:

  1. In the left-hand navigation pane, click on “Databases”.
  2. Click the “Create database” button.

Configure the Database

On the database creation page, select the appropriate options:

  1. Database creation method: Select “Standard create”.
  2. Engine type: Select “Amazon Aurora”.
  3. Aurora edition: Select “Amazon Aurora PostgreSQL-Compatible Edition”.
  4. Capacity settings: Select “Serverless”.
  5. DB cluster name: Enter a name for your database cluster (e.g., aurora-serverless-pg).
  6. Credentials settings: Set a username and password for the database administrator (e.g., admin and a strong password). Keep these credentials handy; you’ll need them to connect.
  7. Capacity configuration: Define the minimum and maximum Aurora Capacity Units (ACUs). This dictates the scaling bounds of the database. Start with lower values (e.g., 0.5 ACU min and 2 ACU max) to keep initial costs down.
  8. Connectivity: Ensure you properly configure the VPC and security group to allow access from your network. It’s a best practice to create a new security group that restricts access to only your local machine or a specific VPC.
  9. Additional configuration: Configure settings like backups, monitoring, and maintenance. Consider enabling Enhanced Monitoring to gain deeper performance insights.

Finalize Creation

After configuring all options, review your settings and click “Create database”. Provisioning will take a few minutes. You can monitor the status directly in the RDS console.

Connecting to the Database

Once the database is provisioned, you can connect to it using a PostgreSQL client.

Get the Database Endpoint

To connect to the database, you’ll need the cluster endpoint.

  1. In the RDS console, select your database cluster (aurora-serverless-pg).
  2. Under the “Connectivity & security” section, find the cluster endpoint (Writer endpoint). Copy this endpoint.

Configure the Security Group

Ensure the security group associated with the database allows inbound traffic from your machine or network.

  1. In the RDS console, select your database cluster.
  2. Under “Connectivity & security”, click the link to the security group.
  3. In the security group settings, add an inbound rule allowing PostgreSQL traffic (port 5432) from your IP address or network.

Connect Using psql

Use the psql client to connect to the database. Open a terminal and run the following command, replacing <endpoint>, <username>, and <password> with your actual values:

psql -h <endpoint> -U <username> -p 5432 -d postgres

For example:

psql -h aurora-serverless-pg.xxxxxxxxxxxx.us-east-1.rds.amazonaws.com -U admin -p 5432 -d postgres

You will be prompted for the password. Enter the password you set during database creation.

If the connection is successful, you should see the psql prompt:

psql (14.7)
Type "help" for help.

postgres=>

You can now run SQL queries against your Aurora Serverless PostgreSQL database.

Test a Query

Run a test query to verify the database is functioning correctly:

SELECT version();

You should see output similar to this:

                                                     version
----------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.7 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-15), 64-bit
(1 row)

Scaling and Monitoring

A core feature of Aurora Serverless is its ability to scale automatically based on workload demands.

Monitor Capacity

You can monitor your database’s capacity utilization directly in the RDS console.

  1. In the RDS console, select your database cluster.
  2. Under the “Monitoring” tab, you can view graphs displaying CPU utilization, memory, and database capacity over time.

Test Scaling

To test auto-scaling, simulate a heavy workload on the database. For instance, you can execute a series of complex queries or bulk load data. Watch how the database capacity automatically ramps up to handle the load.

Configure Scaling

You can modify the scaling settings (minimum and maximum capacity) in the RDS console.

  1. In the RDS console, select your database cluster.
  2. Click “Modify”.
  3. Under “Capacity configuration”, adjust the minimum and maximum capacity values.

Cost Optimization

Aurora Serverless PostgreSQL is designed to optimize costs, but it’s crucial to monitor usage and configure settings properly to maximize savings.

Monitor Costs

Use AWS Cost Explorer to track the costs associated with your Aurora Serverless database. Analyze spending to identify potential areas for optimization.

Configure Idle Timeout

Aurora Serverless automatically pauses the database if it remains idle for a specified period. You can configure this idle timeout in the RDS console.

  1. In the RDS console, select your database cluster.
  2. Click “Modify”.
  3. Under “Additional configuration”, set the idle timeout. A lower timeout reduces costs but may introduce a slight delay (cold start) when the database resumes.

Best Practices for Cost Optimization

  • Set an appropriate minimum capacity for your workload.
  • Configure a sensible idle timeout.
  • Regularly monitor database costs and usage.
  • Consider reserved instances or Savings Plans to further reduce costs. When I deployed this for a public healthcare organization with 2,000 endpoints, optimizing the idle timeout resulted in significant cost savings.

Common Errors and Troubleshooting

  • Connection Error: “Connection timed out” or “Connection refused”:

* Cause: The security group is blocking access from your machine or network.

* Solution: Verify the security group’s inbound rules and ensure it allows PostgreSQL traffic (port 5432) from your IP address or network.

  • Authentication Error: “Password authentication failed”:

* Cause: Incorrect credentials (username or password).

* Solution: Double-check your username and password. If you forgot your password, you can reset it in the RDS console.

  • Database not auto-scaling as expected:

* Cause: The maximum capacity is set too low, or the workload isn’t generating enough load.

* Solution: Increase the maximum capacity limit and simulate a heavier workload to test scaling.

Conclusion

In this tutorial, you learned how to create, connect to, and manage an Amazon Aurora Serverless PostgreSQL database. You discovered how to leverage its auto-scaling capabilities, cost optimization features, and management simplicity. Remember to regularly monitor costs and usage to maximize your savings. Aurora Serverless PostgreSQL is an excellent solution for a wide range of applications, from variable workloads to dev/test environments.

Key takeaways:

  • Aurora Serverless PostgreSQL automatically adapts to your application’s needs, scaling capacity based on workload demands.
  • You only pay for what you use, optimizing costs compared to traditional database instances.
  • Setup is quick and simple, allowing you to spin up a database in seconds.

Have questions or comments? Drop them below!

Share this article:

Written by

Rosario Giordano

Rosario Giordano is a system administrator and IT consultant specializing in cybersecurity and cloud, with over 20 years of experience managing enterprise Linux infrastructures. His areas of expertise include SSH hardening, Kubernetes platforms, PostgreSQL databases, VMware/ Proxmox virtualization, and compliance with NIS2 and ISO 27001 security frameworks