Skip to main content

It generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions.

Project description

psql-script-generator

It generates a SQL script based on a Jinja2 template, allowing you to automate the creation of a database, roles, and permissions. It is particularly useful for setting up a PostgreSQL database with predefined roles and permissions.

Installation:

# You can create a virtual env to install the cli

# Creating the venv
$ python -m venv tutorial_env

# Activating
$ source tutorial_env/bin/activate

# Installing the cli
pip install psql-script-generator

Usage

$ psql-script-generator -d <database_name> -w <readwrite_role> -u <user_role> -p <password> -t <template_file> -o <output_file>
-d, --database: Name of the database.
-w, --readwrite_role: Name of the readwrite role.
-u, --user_role: Name of the users role.
-p, --password: Password for the user roles.
-t, --template: Path to the Jinja2 template file.
-o, --output_file: Output file for the generated SQL script.

Let's validate our generated sql script!!!

Start a PostgreSQL container:

$ docker run --name psql-validating -e POSTGRES_PASSWORD=mysecretpassword -p 5555:5432 -d postgres:13

Run generate_sql_script cli:

$ psql-script-generator -d test -w test_readwrite -u test_user -p 'qweasdzxc' -t readwrite-user-template.sql.j2 -o test_sql_script.sql

Run the generated SQL script:

$ export PGPASSWORD='mysecretpassword'; psql -h localhost -U postgres -d postgres -p 5555 -w -f test_sql_script.sql

# The output should be something like that:

CREATE DATABASE
CREATE ROLE
GRANT
GRANT
GRANT
GRANT
GRANT
GRANT
GRANT
ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES
ALTER DEFAULT PRIVILEGES
CREATE ROLE
GRANT ROLE
ALTER DATABASE

Now, let's test the grants that provided to test_user

# Download the validating.sql file
$ curl -o validating.sql https://raw.githubusercontent.com/pledo/psql-script-generator/main/validating.sql

# Run the sql script with write, read and delete permissions
export PGPASSWORD='qweasdzxc'; psql -h localhost -U test_user -d postgres -p 5555 -w -f validating.sql

# The output should be something like:

CREATE TABLE
INSERT 0 1
 id |         title
----+------------------------
  1 | Learn basic SQL syntax
(1 row)

ALTER TABLE
UPDATE 1
 id |         title          | completed
----+------------------------+-----------
  1 | Learn basic SQL syntax | t
(1 row)

INSERT 0 1
DELETE 1
 id |         title          | completed
----+------------------------+-----------
  1 | Learn basic SQL syntax | t
(1 row)

DROP TABLE

Uninstall cli

$ pip uninstall psql-script-generator

and deactive the venv

$ deactivate

For a full automated tests

Enter tests folder

$ cd tests

Run the bash script

bash full_test.sh

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

psql_script_generator-0.0.3.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

psql_script_generator-0.0.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file psql_script_generator-0.0.3.tar.gz.

File metadata

  • Download URL: psql_script_generator-0.0.3.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.4

File hashes

Hashes for psql_script_generator-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c82713a607d526918185d3bc36eee30ecf6d0b92548c34172716bd9cf2dff995
MD5 26c6691f11feae776a9f0cf455f2885d
BLAKE2b-256 c3e1011b46b7437a5b0e0689cf4d90409e785d652082f43382a30bf045f994cb

See more details on using hashes here.

File details

Details for the file psql_script_generator-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for psql_script_generator-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b0b3e787cc0583d7ced580cd388fc4744289adfe03c84aad99713def7dcbc23
MD5 c3a336281843713e06655877c75acd4d
BLAKE2b-256 332b50d8e582f638b9f5f366fab5756e72ba32057b3132051b34c4c2f419324b

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