Skip to main content

Execute a SQL file on all PostgreSQL databases on a host.

Project description

crossdb-query

python tests docker build pypi publish

Execute a SQL file on all PostgreSQL databases on a host. By default, the script will execute the SQL file on all databases on the host. The user can specify a list of databases to include or exclude from the execution. By default, the postgres database is excluded from the execution as well as any template databases.

Features

  • Execute SQL files on multiple PostgreSQL databases.
  • Include or exclude specific databases.
  • List all available databases on the host.
  • Debug logging support.

Installation

Using Docker

  1. Build the Docker image:

    docker build -t crossdb-query .
    
  2. Run the Docker container and bind mount the SQL file:

    docker run -it --rm -v $(pwd)/<sql_file>:/app/<sql_file> crossdb-query -u <username> -v <host> -f <sql_file>
    

Using Python

  1. Install using pip:

    pip install crossdb-query
    
  2. Run the script:

    crossdb_query.py -u <username> -v <host> -f <sql_file>
    

Usage

usage: crossdb_query.py [-h] [-d] [-f FILE] -u USER -v HOST [-p PORT] [-l] [-i INCLUDE] [-e EXCLUDE]

Execute a SQL file on all PostgreSQL databases on a host. By default, the script will execute the SQL file on all databases on the host. The user can specify a list of databases to include or exclude from the execution.

options:
  -h, --help            show this help message and exit
  -d, --debug           Enable debug logging.
  -f FILE, --file FILE  The SQL file to execute.
  -u USER, --user USER  The username to connect to the database.
  -v HOST, --host HOST  The host of the database.
  -p PORT, --port PORT  The port of the database.
  -l, --list            List all available databases on the host and exit.
  -i INCLUDE, --include INCLUDE
                        A comma-separated list of databases on which the SQL file will be executed. THe databases must be separated by a comma and no spaces.
  -e EXCLUDE, --exclude EXCLUDE
                        A comma-separated list of databases on which the SQL file will not be executed. The databases must be separated by a comma and no spaces.

Examples

List all databases on the host

$ crossdb_query.py -u <username> -v <host> -l

The script crossdb_query.py wants the password for PostgresDB(host=localhost, port=5432, database=postgres, user=pg_user):
Available databases:
  - accounts
  - customers
  - django_db
  - employees

Execute a SQL file on selected databases

$ crossdb_query.py -u <username> -v <host> -f <sql_file> -i accounts,employees

The script crossdb_query.py wants the password for PostgresDB(host=localhost, port=5432, database=postgres, user=pg_user):
Databases selected for execution:
  - accounts
  - employees
(1/2) Executing on database accounts
  Success
(2/2) Executing on database employees
  Success

Execute a SQL file on all databases except the specified ones

$ crossdb_query.py -u <username> -v <host> -f <sql_file> -e accounts,employees

The script crossdb_query.py wants the password for PostgresDB(host=localhost, port=5432, database=postgres, user=pg_user):
Databases selected for execution:
  - customers
  - django_db
(1/2) Executing on database customers
  Success
(2/2) Executing on database django_db
  Success

Notes

If the user specifies a database that does not exist on the host, the script will ignore the database and continue with the script execution without warning the user.

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

crossdb_query-0.0.2.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

crossdb_query-0.0.2-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file crossdb_query-0.0.2.tar.gz.

File metadata

  • Download URL: crossdb_query-0.0.2.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for crossdb_query-0.0.2.tar.gz
Algorithm Hash digest
SHA256 abed8f718054f2a01f87b3c438b0df8839a73ed9de1f3fef4e7cfd177e8e8746
MD5 0ac4c702ffa0fb25ff704ca2718e975b
BLAKE2b-256 76e3a69929e2b6366a7c56c3ab4440139fdbf2bfe57d96b4cb97ab3b11357734

See more details on using hashes here.

File details

Details for the file crossdb_query-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for crossdb_query-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c50f9ef56f77f68b24dbc7f6c2a73a147d995708a7f7388636a16c2051aaa364
MD5 40911decc54fc6bcdc1ee77386fa180c
BLAKE2b-256 f0367e11c09fb9971ff2380a1914b23f91956bfde71cc699fbd1be5790650362

See more details on using hashes here.

Supported by

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