Skip to main content

Parse PostgreSQL DSNs into PG* environment variables with export helpers.

Project description

pgdsn2env

Parse PostgreSQL DSN strings into PG* environment variables and emit shell-friendly outputs.

Install

uv add pgdsn2env
# or
pip install pgdsn2env

Optional DSN parsers:

uv add "pgdsn2env[psycopg3]"
# or
uv add "pgdsn2env[psycopg2]"
# or
pip install "pgdsn2env[psycopg3]"
# or
pip install "pgdsn2env[psycopg2]"

CLI usage

# Basic pgdsn2env "postgresql://user@location:5432/test"
export PGDATABASE=test
export PGHOST=location
export PGPORT=5432
export PGUSER=user

# With Keyword/Value Connection Strings, in JSON format  pgdsn2env "host=localhost port=5432 dbname=mydb user=postgres password=secret" --format json
{"PGDATABASE": "mydb", "PGHOST": "localhost", "PGPASSWORD": "secret", "PGPORT": "5432", "PGUSER": "postgres"}

# Invoked as Python module, with advanced params python -m pgdsn2env --format dotenv "postgresql://postgres:secret@localhost:5432/mydb?sslmode=require&application_name=myapp"
PGAPPNAME=myapp
PGDATABASE=mydb
PGHOST=localhost
PGPASSWORD=secret
PGPORT=5432
PGSSLMODE=require
PGUSER=postgres

Python usage

from pgdsn2env import pg_dsn_to_env

pg_env = pg_dsn_to_env("postgresql://user@location:5432/test")
print(pg_env) # {'PGHOST': 'location', 'PGPORT': '5432', 'PGDATABASE': 'test', 'PGUSER': 'user'}

Recipes

Set PG* environment variables in your current shell:

source <(pgdsn2env "$DATABASE_URL")

Then verify the variables are set:

echo $PGHOST $PGPORT $PGDATABASE $PGUSER

Notes

  • A DSN parser is required: install either psycopg or psycopg2.
  • Only parameters present in the DSN are emitted.
  • Values are shell-quoted in CLI output.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgdsn2env-1.0.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgdsn2env-1.0.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file pgdsn2env-1.0.1.tar.gz.

File metadata

  • Download URL: pgdsn2env-1.0.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pgdsn2env-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a77c7e94bb0f5fe0f6a6fb314219d3a4ef47df53a0090a6d3f454bf716fff15c
MD5 962860cf915c0c2ceeda089e61ea34d3
BLAKE2b-256 95d1a56cae2e79936beb78d722955574af5b949ec2a0543963123398616d813f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgdsn2env-1.0.1.tar.gz:

Publisher: cd.yml on kafai-lam/pgdsn2env

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pgdsn2env-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pgdsn2env-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pgdsn2env-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ace72663766fe2f7384e844f6399207cb37e172b67d96f54b1f14c09a83ac3e
MD5 87b60faa18a4f3f13a0ea41ebd43cecc
BLAKE2b-256 ef1494a57dcf792ded7f4b54418126ab88470a63651574ef3a5a9af0433aedf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgdsn2env-1.0.1-py3-none-any.whl:

Publisher: cd.yml on kafai-lam/pgdsn2env

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page