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.0.tar.gz (3.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.0.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pgdsn2env-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pgdsn2env-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bb87591cc85583f3bcd115c2dba25e0f682d2afb831442a32719e61e3101f02e
MD5 1f21654394dc7fb3a4c1c1442cdaf436
BLAKE2b-256 c85b5d26d19f10e704ad5883ae52668225081e7b5f5a10b24a4007e112bb5efd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgdsn2env-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.25 {"installer":{"name":"uv","version":"0.9.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pgdsn2env-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3498e3132fe6e397b3282e0dccee3a155157891e45c508ae6ab1fe3c9c2ed8b
MD5 90c7bed2dda618934f052fa5ad38cbea
BLAKE2b-256 1dc6dc21075e54c6d541d9a8124db4bf8e22cf925022eaeb17c98c5765e09f63

See more details on using hashes here.

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