MSDN Azure Tutorials

Development with Azure Database for PostgreSQL

This tutorial will guide you through the process of developing applications using Azure Database for PostgreSQL. We'll cover setting up your environment, connecting to your database, and common development tasks.

1. Setting Up Azure Database for PostgreSQL

Before you start developing, you need to provision an Azure Database for PostgreSQL instance. This can be done through the Azure portal, Azure CLI, or ARM templates.

Steps:

  1. Navigate to the Azure portal.
  2. Search for "Azure Database for PostgreSQL".
  3. Click "Create".
  4. Choose a deployment option (Single server, Flexible Server, or Hyperscale Citus).
  5. Fill in the required details like server name, admin login, password, region, and pricing tier.
  6. Review and create the server.

Once deployed, you'll have a fully managed PostgreSQL server ready for your applications.

2. Connecting to Your PostgreSQL Instance

Connecting to your Azure Database for PostgreSQL instance requires specifying connection details such as the server name, database name, username, and password. You can use various client tools and programming languages.

Using `psql` (Command-Line Tool)

The `psql` command-line utility is a powerful tool for interacting with PostgreSQL databases.

psql "host=.postgres.database.azure.com port=5432 dbname= user= password= sslmode=require"

Replace the placeholders with your actual server details.

Connecting from a Programming Language (Example: Python with `psycopg2`)

Ensure you have the `psycopg2` library installed (`pip install psycopg2-binary`).

import psycopg2 # Connection parameters host = ".postgres.database.azure.com" database = "" user = "" password = "" sslmode = "require" try: conn = psycopg2.connect(host=host, database=database, user=user, password=password, sslmode=sslmode) print("Successfully connected to Azure Database for PostgreSQL!") # Create a cursor object to interact with the database cur = conn.cursor() # Example query cur.execute("SELECT version();") db_version = cur.fetchone() print(f"PostgreSQL database version: {db_version[0]}") # Close communication with the database cur.close() except Exception as e: print(f"Error connecting to the database: {e}") finally: if 'conn' in locals() and conn is not None: conn.close()

3. Working with Tables and Data

Once connected, you can perform standard SQL operations like creating tables, inserting, updating, and deleting data.

Creating a Sample Table

CREATE TABLE IF NOT EXISTS products ( product_id SERIAL PRIMARY KEY, product_name VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP );

Inserting Data

INSERT INTO products (product_name, price) VALUES ('Laptop', 1200.50); INSERT INTO products (product_name, price) VALUES ('Mouse', 25.99);

Querying Data

SELECT * FROM products; SELECT product_name, price FROM products WHERE price > 1000;

4. Security and Best Practices

Azure Database for PostgreSQL offers robust security features. Ensure you configure firewalls, SSL connections, and manage user roles effectively.

Important

Always use SSL connections to encrypt data in transit. Configure server firewall rules to restrict access to authorized IP addresses only.

5. Next Steps

Explore advanced topics such as replication, performance tuning, integration with Azure services like Azure Functions and App Service, and migrating existing PostgreSQL workloads to Azure.