Skip to main content

Build Status Coverage Status PyPI version GitHub license

pgsu

Connect to an existing PostgreSQL cluster as a PostgreSQL SUPERUSER and execute SQL commands.

psycopg has a great API for interacting with PostgreSQL, once you provide it with the connection parameters for a given database. However, what if your desired database and database user do not yet exist? In order to create them, you will need to connect to PostgreSQL as a SUPERUSER.

Features

  • autodetects postgres setup, tested on
  • uses psycopg to connect if possible
  • can use sudo to become the postgres UNIX user if necessary/possible (default Ubuntu PostgreSQL setups)

Usage

Python API

from pgsu import PGSU
pgsu = PGSU()  # On Ubuntu, this may prompt for sudo password
pgsu.execute("CREATE USER newuser WITH PASSWORD 'newpassword'")
users = pgsu.execute("SELECT usename FROM pg_user WHERE usename='newuser'")
print(users)

While the main point of the package is to guess how to connect as a postgres superuser, you can also provide partial or all information abut the setup using the dsn parameter. These are the default settings:

from pgsu import PGSU
pgsu = PGSU(dsn={
    'host': None,
    'port': 5432,
    'user': 'postgres',
    'password': None,
    'dbname': 'template1',  # Note: you cannot drop databases you are connected to
})

Command line tool

The package also comes with a very basic pgsu command line tool that allows users to execute PostgreSQL commands as the superuser:

$ pgsu "SELECT usename FROM pg_user"
Trying to connect to PostgreSQL...
Executing query: SELECT usename FROM pg_user
[('aiida_qs_leopold',),
 ('postgres',)]

Tests

Run the tests as follows:

pip install -e .[dev]
pytest

Metadata

Release files for pgsu 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgsu 0.3.0
File Size Uploaded
pgsu-0.3.0.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pgsu 0.3.0
File Interpreter ABI Platform
pgsu-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / pgsu-0.3.0.tar.gz

Download URL pgsu-0.3.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
51dbb8f2272deacc28f546af442a1411eb260b1da53e11d4f93ea90f2b1acf50
BLAKE2b-256 checksum
How to use checksums
581d48f83c23a7be0d8bbbab80cb93010ffee0dad2a760d86de3b28470f0b6f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release files / pgsu-0.3.0-py3-none-any.whl

Download URL pgsu-0.3.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bb206d02ea6bd8388cb3998e12f1ca9e954a740053b76773a99dc6d55fa23f4
BLAKE2b-256 checksum
How to use checksums
fef03403fae3006cd6c1e64378b7df10d59c114b23a7a90a4b565d8389cb4cb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.3

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page