{"id":1440,"library":"dagster-postgres","title":"Dagster PostgreSQL Integration","description":"dagster-postgres is an integration library for Dagster that provides resources and storage implementations for interacting with PostgreSQL databases. It allows Dagster assets to connect to Postgres, and can also use Postgres for Dagster's run storage and event log storage. It is currently at version 0.28.22 and follows the rapid release cycle of the main Dagster project.","status":"active","version":"0.28.22","language":"en","source_language":"en","source_url":"https://github.com/dagster-io/dagster/tree/master/python_modules/libraries/dagster-postgres","tags":["dagster","postgres","etl","orchestration","database","data-platform"],"install":[{"cmd":"pip install dagster-postgres","lang":"bash","label":"Install library"}],"dependencies":[{"reason":"dagster-postgres is an integration library for the Dagster orchestration framework and requires the core `dagster` library.","package":"dagster","optional":false},{"reason":"Required for database connectivity if you were relying on it as a transitive dependency before version 0.28.18; now must be explicitly installed.","package":"psycopg2-binary","optional":true}],"imports":[{"symbol":"postgres_resource","correct":"from dagster_postgres import postgres_resource"},{"note":"Used for configuring Dagster's run storage in `dagster.yaml`.","symbol":"PostgresRunStorage","correct":"from dagster_postgres.run_storage import PostgresRunStorage"},{"note":"Used for configuring Dagster's event log storage in `dagster.yaml`.","symbol":"PostgresEventLogStorage","correct":"from dagster_postgres.event_log import PostgresEventLogStorage"}],"quickstart":{"code":"import os\nfrom dagster import Definitions, asset, EnvVar\nfrom dagster_postgres import postgres_resource\n\n@asset(compute_kind=\"sql\")\ndef my_postgres_asset(postgres: postgres_resource):\n    \"\"\"A simple asset that connects to PostgreSQL and runs a dummy query.\"\"\"\n    with postgres.get_connection() as conn:\n        with conn.cursor() as cursor:\n            cursor.execute(\"SELECT 1;\")\n            result = cursor.fetchone()\n            print(f\"Postgres query result: {result}\")\n            # In a typical asset, you'd load/transform/store data here.\n\ndefs = Definitions(\n    assets=[my_postgres_asset],\n    resources={\n        \"postgres\": postgres_resource.configured({\n            \"database\": EnvVar(\"POSTGRES_DB\"),\n            \"host\": EnvVar(\"POSTGRES_HOST\"),\n            \"port\": EnvVar(\"POSTGRES_PORT\"),\n            \"user\": EnvVar(\"POSTGRES_USER\"),\n            \"password\": EnvVar(\"POSTGRES_PASSWORD\"),\n        })\n    }\n)\n\n# To run this example, set environment variables (e.g., in your shell):\n# export POSTGRES_DB=your_db_name\n# export POSTGRES_HOST=localhost\n# export POSTGRES_PORT=5432\n# export POSTGRES_USER=your_user\n# export POSTGRES_PASSWORD=your_password\n# Then execute using `dagster dev -f your_file.py` and materialize `my_postgres_asset` in the UI.","lang":"python","description":"This example defines a simple Dagster asset that uses `postgres_resource` to connect to a PostgreSQL database. It demonstrates how to configure the resource using environment variables and then obtain a connection to execute a query. Ensure you have the necessary PostgreSQL environment variables set for the connection to succeed."},"warnings":[{"fix":"Explicitly add `psycopg2-binary` or `psycopg2` to your project's dependencies: `pip install psycopg2-binary`","message":"The `psycopg2-binary` package was removed as a transitive dependency in `dagster-postgres` version 0.28.18 (Dagster core 1.12.18). If your project relied on this transitive dependency, you must now explicitly install `psycopg2-binary` (or `psycopg2`) in your environment.","severity":"gotcha","affected_versions":">=0.28.18"},{"fix":"Always install `dagster-postgres` with the same major.minor version as your core `dagster` package (e.g., if `dagster==1.12.x`, install `dagster-postgres==0.28.x`). Use `pip install dagster==X.Y.Z dagster-postgres==0.X.Y`.","message":"Dagster library versions (e.g., `dagster-postgres`) are tightly coupled with the core `dagster` library version. Mismatched versions can lead to unexpected behavior or runtime errors. Always ensure all `dagster-*` packages in your environment match the major and minor version of your core `dagster` installation.","severity":"gotcha","affected_versions":"*"},{"fix":"Use `EnvVar()` for programmatic configuration or `${MY_SECRET_ENV_VAR}` syntax in `dagster.yaml` to pull credentials from environment variables.","message":"When configuring `postgres_resource`, sensitive credentials (user, password) should always be managed securely, ideally using environment variables or a secret management system, rather than hardcoding them directly in code or `dagster.yaml`.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}