Develop a Python script that connects to a PostgreSQL database, creates a table called "users" with columns for id, name, email, and age, and inserts five sample records into the table. Then, write a query to retrieve all users whose age is greater than 30 and print their names and emails.
PLAINFORMAT
import psycopg2

# Replace these values with your own PostgreSQL connection details
db_name = "your_database_name"
db_user = "your_username"
db_password = "your_password"
db_host = "your_host"
db_port = "your_port"

# Connect to the PostgreSQL database
connection = psycopg2.connect(
    dbname=db_name,
    user=db_user,
    password=db_password,
    host=db_host,
    port=db_port
)

cursor = connection.cursor()

# Create the "users" table
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    age INTEGER NOT NULL
)
""")

# Insert five sample records into the "users" table
sample_users = [
    ("Alice", "alice@example.com", 28),
    ("Bob", "bob@example.com", 32),
    ("Charlie", "charlie@example.com", 22),
    ("David", "david@example.com", 45),
    ("Eve", "eve@example.com", 35)
]

for user in sample_users:
    cursor.execute("INSERT INTO users (name, email, age) VALUES (%s, %s, %s)", user)

# Commit the changes
connection.commit()

# Retrieve all users whose age is greater than 30
cursor.execute("SELECT name, email FROM users WHERE age > 30")
result = cursor.fetchall()

print("Users older than 30:")
for row in result:
    print(f"Name: {row[0]}, Email: {row[1]}")

# Close the cursor and connection
cursor.close()
connection.close()