Skip to main content

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

Project description

pgdsn2env

CD PyPI Version Python Versions

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

Install

uv add pgdsn2env
# or
pip install pgdsn2env

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

  • Accept both keyword-value pair and URI style PostgreSQL connection strings
  • 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.1.0.tar.gz (4.7 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.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pgdsn2env-1.1.0.tar.gz
Algorithm Hash digest
SHA256 799b6076e454034db80f00126bc8bcf3d72859df8e47b276ea5c113847b68232
MD5 d9b81539923f34b986ed30b4a23146b1
BLAKE2b-256 e568cc390e74cb1e84b53bb8735279fe348e2ae3b1ef4a5c2bffa69a0ddee3f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgdsn2env-1.1.0.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: pgdsn2env-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c64db2454bc7aae1ba3be6b4c0460a30bf38596a867a9499bf079be9ea6f3e71
MD5 4ca709e8fccac3feab18fc2fbdfaf6ff
BLAKE2b-256 74fda174b1f0af1c43b44e8e714c84e4cc37ef285c78f1aeeb749a51fc46b83d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pgdsn2env-1.1.0-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