{
  "name": "sql-to-s3",
  "title": "Run a SQL query and write results to S3 as CSV",
  "category": "Data",
  "tags": [
    "data",
    "python"
  ],
  "version": "1.1.2",
  "date": "2026-10-16",
  "notes": "Repin python on 3.15-slim and move psycopg2-binary 2.9.12 to 2.9.13, the first release with CPython 3.15 wheels. 2.9.12 had none, so pip fell back to building it from source and failed for want of pg_config.",
  "latest": true,
  "installable": true,
  "url": "https://catalog.dagweave.com/api/v1/items/sql-to-s3/1.1.2.json",
  "templateUrl": "https://catalog.dagweave.com/api/v1/items/sql-to-s3/1.1.2/template.yaml",
  "entrypoint": "export",
  "parameters": [
    {
      "name": "bucket",
      "title": "Bucket",
      "description": "Target S3 bucket for the CSV result (no s3:// prefix).",
      "type": "string",
      "required": true,
      "example": "my-data-bucket"
    },
    {
      "name": "key",
      "title": "Object key",
      "description": "Destination object key for the CSV result.",
      "type": "string",
      "required": true,
      "example": "exports/users.csv"
    },
    {
      "name": "query",
      "title": "SQL query",
      "description": "SQL query to run against the Postgres database.",
      "type": "string",
      "required": true,
      "example": "SELECT id, email FROM users WHERE active"
    },
    {
      "name": "aws-access-key-id-key",
      "title": "Access key id secret key",
      "description": "Key within the Secret that holds the AWS access key id.",
      "type": "string",
      "required": false,
      "default": "access-key-id"
    },
    {
      "name": "aws-secret-access-key-key",
      "title": "Secret access key secret key",
      "description": "Key within the Secret that holds the AWS secret access key.",
      "type": "string",
      "required": false,
      "default": "secret-access-key"
    },
    {
      "name": "aws-secret-name",
      "title": "AWS secret name",
      "description": "Name of the Kubernetes Secret in the run namespace holding the AWS credentials.",
      "type": "string",
      "required": false,
      "default": "aws-credentials"
    },
    {
      "name": "db-secret-key",
      "title": "DB secret key",
      "description": "Key within the Secret that holds the Postgres connection string.",
      "type": "string",
      "required": false,
      "default": "connection-string"
    },
    {
      "name": "db-secret-name",
      "title": "DB secret name",
      "description": "Name of the Kubernetes Secret holding the Postgres connection string (DSN / libpq URL).",
      "type": "string",
      "required": false,
      "default": "postgres-credentials"
    },
    {
      "name": "region",
      "title": "AWS region",
      "description": "AWS region of the bucket.",
      "type": "string",
      "required": false,
      "default": "us-east-1"
    }
  ],
  "images": [
    {
      "ref": "python@sha256:94d3426f1def7db4a106a232ac121bfd975393e5dfd40a619b955fae45fe7913",
      "repository": "python",
      "digest": "sha256:94d3426f1def7db4a106a232ac121bfd975393e5dfd40a619b955fae45fe7913"
    }
  ],
  "install": {
    "kubectl": "kubectl apply -n argo -f https://catalog.dagweave.com/api/v1/items/sql-to-s3/1.1.2/template.yaml",
    "argo": "argo template create -n argo https://catalog.dagweave.com/api/v1/items/sql-to-s3/1.1.2/template.yaml"
  },
  "template": "apiVersion: argoproj.io/v1alpha1\nkind: WorkflowTemplate\nmetadata:\n  name: sql-to-s3\n  labels:\n    dagweave.io/catalog-item: sql-to-s3\n  annotations:\n    # The catalog semver this manifest was authored at. The dagweave app records\n    # (catalog-item-id, semver) -\u003e content-hash -\u003e base-vN when it mints the tenant\n    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.\n    dagweave.io/catalog-version: \"1.1.2\"\n    dagweave.io/catalog-category: \"Data\"\nspec:\n  entrypoint: export\n  templates:\n    - name: export\n      inputs:\n        parameters:\n          # SQL query to run against the Postgres database.\n          - name: query\n          # Target S3 bucket and key for the CSV result (no s3:// prefix on bucket).\n          - name: bucket\n          - name: key\n          # AWS region of the bucket.\n          - name: region\n            default: us-east-1\n          # Name of the Kubernetes Secret holding the Postgres connection string\n          # (DSN / libpq URL), plus the key within it.\n          - name: db-secret-name\n            default: postgres-credentials\n          - name: db-secret-key\n            default: connection-string\n          # Name of the Kubernetes Secret holding the AWS credentials, plus the keys\n          # within it. Defaults let a user leave them blank in the Inspector.\n          - name: aws-secret-name\n            default: aws-credentials\n          - name: aws-access-key-id-key\n            default: access-key-id\n          - name: aws-secret-access-key-key\n            default: secret-access-key\n      container:\n        # Pinned by digest per the catalog convention: an image rebuild can never\n        # silently change the behaviour of an already-released item version.\n        # python:3.15-slim\n        #\n        # The one item in the catalog whose base cannot run it: it needs a Postgres\n        # driver and an S3 client, and this image has neither, so both are installed\n        # at run time from PyPI. Pinned versions, because a digest that freezes the\n        # image while the code inside it floats is a digest that promises nothing.\n        # ./Dockerfile is where that install belongs and where it goes once\n        # ghcr.io/dagweave/catalog/sql-to-s3 is published and public; keep the two\n        # version pins in step until then. See docs/adr/0001.\n        image: python@sha256:94d3426f1def7db4a106a232ac121bfd975393e5dfd40a619b955fae45fe7913\n        command: [sh, -c]\n        # The quoted heredoc (\u003c\u003c'PY') passes the program to python literally — no\n        # shell expansion — and every user value (query, bucket, key, credentials)\n        # is read from an env var via os.environ, never interpolated into code.\n        args:\n          - |\n            set -eu\n            pip install --quiet --no-cache-dir psycopg2-binary==2.9.13 boto3==1.43.68\n            python - \u003c\u003c'PY'\n            import os, csv, io, boto3, psycopg2\n            conn = psycopg2.connect(os.environ[\"DATABASE_DSN\"])\n            cur = conn.cursor()\n            cur.execute(os.environ[\"SQL_QUERY\"])\n            buf = io.StringIO()\n            w = csv.writer(buf)\n            if cur.description:\n                w.writerow([d[0] for d in cur.description])\n                for row in cur:\n                    w.writerow(row)\n            cur.close()\n            conn.close()\n            body = buf.getvalue().encode(\"utf-8\")\n            boto3.client(\"s3\").put_object(\n                Bucket=os.environ[\"S3_BUCKET\"],\n                Key=os.environ[\"S3_KEY\"],\n                Body=body,\n            )\n            print(\"OK: wrote %d bytes to s3://%s/%s\" % (\n                len(body), os.environ[\"S3_BUCKET\"], os.environ[\"S3_KEY\"]))\n            PY\n        env:\n          - name: SQL_QUERY\n            value: \"{{inputs.parameters.query}}\"\n          - name: S3_BUCKET\n            value: \"{{inputs.parameters.bucket}}\"\n          - name: S3_KEY\n            value: \"{{inputs.parameters.key}}\"\n          - name: AWS_DEFAULT_REGION\n            value: \"{{inputs.parameters.region}}\"\n          - name: DATABASE_DSN\n            valueFrom:\n              secretKeyRef:\n                name: \"{{inputs.parameters.db-secret-name}}\"\n                key: \"{{inputs.parameters.db-secret-key}}\"\n          - name: AWS_ACCESS_KEY_ID\n            valueFrom:\n              secretKeyRef:\n                name: \"{{inputs.parameters.aws-secret-name}}\"\n                key: \"{{inputs.parameters.aws-access-key-id-key}}\"\n          - name: AWS_SECRET_ACCESS_KEY\n            valueFrom:\n              secretKeyRef:\n                name: \"{{inputs.parameters.aws-secret-name}}\"\n                key: \"{{inputs.parameters.aws-secret-access-key-key}}\"\n",
  "templateSha256": "5314220cb0ee7c83587b6ac98c97897d7681e2a616d6b47c0a7088bd268df9c1",
  "source": "https://github.com/dagweave/catalog/tree/67458f74dbbf913b03177f3cefc69424be85eb1d/catalog/sql-to-s3",
  "catalogSha": "67458f74dbbf913b03177f3cefc69424be85eb1d"
}
