Skip to main content

About

dbsetupmate is a Python package and CLI, which overtakes a role of a database mate. Primary purpose is to create and maintain database schemas and users.

Install

Installation using uv

uv pip install dbsetupmate

Copy sample.env to .env and adjust the POSTGRESQL_* values.

Using Python package as CLI

  • Show the available commands and options
    dbsetupmate --help
    
  • Create a database together with its owner and login user
    dbsetupmate --env-file .env postgresql create-db --new-db-name course_db_01 --new-db-user course_user_01
    
  • Next free generated database name
    dbsetupmate --env-file .env postgresql show-next-db-name
    
  • Create the shared database
    dbsetupmate --env-file .env postgresql create-shared-db
    
  • Read-only user for the shared database
    dbsetupmate --env-file .env postgresql create-shared-user-readonly
    
  • Grant an existing user read-only access to the shared database
    dbsetupmate --env-file .env postgresql grant-shared-access --user-name course_user_01
    
  • Revoke that access again
    dbsetupmate --env-file .env postgresql revoke-shared-access --user-name course_user_01
    
  • List the databases, the users and the resolved settings
    dbsetupmate --env-file .env postgresql show-dbs
    dbsetupmate --env-file .env postgresql show-users
    dbsetupmate --env-file .env postgresql show-config
    
  • Change a user password
    dbsetupmate --env-file .env postgresql set-user-password --user-name course_user_01
    
  • Drop a database together with its roles
    dbsetupmate --env-file .env postgresql drop-db --db-name course_db_01 --db-user course_user_01
    
  • Drop a user
    dbsetupmate --env-file .env postgresql drop-user --user-name course_user_01
    
  • There is also a dry-run option for all commands
      dbsetupmate --env-file .env --dry-run postgresql create-db
    

P.S. The password is prompted for when --new-db-password or --password is omitted. Commands exit 1 on failure. drop-db and drop-user ask for confirmation; pass --yes to skip it.

Using Python package

Here is a example how to use dbsetupmate as Python library.

from dbsetupmate import PostgresMate, PostgreSQLConfig, DBSetupMateException

mate = PostgresMate(PostgreSQLConfig(host="db.internal", admin_password="..."))

try:
    created = mate.create_db("course_db_01", "course_user_01", "s3cret")
except DBSetupMateException as ex:
    print(ex)

PostgreSQLConfig.from_env() reads the POSTGRESQL_* variables instead. Failures raise a subclass of DBSetupMateException, never a bool.

A fuller workflow — create a user, ensure the shared database exists, and grant that user read-only access to it:

from dbsetupmate import PostgresMate, PostgreSQLConfig, DBSetupMateException

mate = PostgresMate(PostgreSQLConfig.from_env())
config = mate.config

try:
    # 1. Create a user together with its own database.
    mate.create_db("course_db_01", "course_user_01", "s3cret")

    # 2. + 3. Create the shared database only if it is not there yet.
    if not mate.database_exists(config.shared_db):
        mate.create_shared_db()

    # 4. Give the new user read-only access to the shared database.
    mate.grant_shared_access("course_user_01")
except DBSetupMateException as ex:
    print(ex)

Development: Setup

This guide walks through setting up the project for local development using uv.

  1. Create a new virtual environment in a .venv directory and activates it.
    uv venv
    
  2. Activate the environment (macOS/Linux):
    source .venv/bin/activate
    
  3. Activate the environment (Windows):
    call .venv/Scripts/activate.bat
    
  4. Install package in editable mode with dev dependencies Installing the package in editable mode (-e) is the key to development.
    uv pip install -e . --group dev
    

Development: Running Tests

task py:pytest              # unit tests, no database needed
task py:pytest-integration  # against live PostgreSQL 15..18 (see compose-tests.yaml)

License

MIT

Download files

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

Source Distribution

dbsetupmate-0.8.0.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

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

dbsetupmate-0.8.0-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file dbsetupmate-0.8.0.tar.gz.

File metadata

  • Download URL: dbsetupmate-0.8.0.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dbsetupmate-0.8.0.tar.gz
Algorithm Hash digest
SHA256 a33186f36dfef69e5880dea2e6398731b766d620736e4b37b4f8e85498630eb2
MD5 fae3408281fce4c75d112bd7c576c4db
BLAKE2b-256 dda151c9404978eb1a248f909157ef08512bf64528450415ca690de00d5f7416

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbsetupmate-0.8.0.tar.gz:

Publisher: publish-to-pypi.yml on vdmitriyev/dbsetupmate

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

File details

Details for the file dbsetupmate-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: dbsetupmate-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dbsetupmate-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d506d5103af73641cae6513ffbe7ac6bd099c8f0257d32cb016d8c1d533df863
MD5 8362d445a07505480afc3ba097adb312
BLAKE2b-256 7662de707f169692233960bc187671550869b7206bf35a382633c7e7eca23563

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbsetupmate-0.8.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on vdmitriyev/dbsetupmate

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 Sentry Error logging StatusPage Status page