Add a Database
How to add a dedicated database to your Shuttle project.
Last updated
from typing import Annotated
import shuttle_task
import shuttle_runtime
from shuttle_aws.rds import RdsPostgres, RdsPostgresOptions
@shuttle_task.cron(schedule="0 * * * ? *") # This task will run hourly
async def main(
postgres: Annotated[
RdsPostgres,
RdsPostgresOptions(), # Default options for a basic Postgres instance
],
):
"""An example task that interacts with a dedicated Postgres database."""
print("Accessing dedicated Postgres database...")
# Get a database connection
# The connection object returned is compatible with psycopg's connection interface
# For async contexts, you might use asyncpg, and get_connection() would return an asyncpg connection.
try:
conn = postgres.get_connection()
with conn.cursor() as cur:
# Example: Create a simple table if it doesn't exist
cur.execute("CREATE TABLE IF NOT EXISTS shuttle_test (id SERIAL PRIMARY KEY, message VARCHAR(255));")
conn.commit()
print("Table 'shuttle_test' ensured.")
# Example: Insert data
cur.execute("INSERT INTO shuttle_test (message) VALUES (%s);", ("Hello from Shuttle Postgres!",))
conn.commit()
print("Successfully inserted data.")
# Example: Select data
cur.execute("SELECT message FROM shuttle_test ORDER BY id DESC LIMIT 1;")
result = cur.fetchone()
print(f"Retrieved from DB: {result[0]}")
except Exception as e:
print(f"Error interacting with Postgres database: {e}")
# This line is essential for Shuttle to run your application locally or deploy
if __name__ == "__main__":
shuttle_runtime.main(main)