Skip to main content

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

Project description

Build Status Coverage Status PyPI version GitHub license

pgsu

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

psycopg2 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 psycopg2 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,
    'database': '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 .[testing]
pytest

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

pgsu-0.2.1.tar.gz (8.3 kB view details)

Uploaded Source

File details

Details for the file pgsu-0.2.1.tar.gz.

File metadata

  • Download URL: pgsu-0.2.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for pgsu-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5e7688689e5a0b60650d9609d6c91534d4017ae8c0fbcbb4a85df9ac4c03688d
MD5 3d7480f317875bf738a2155fa3874e8a
BLAKE2b-256 60c5981ff1983f419c9e7560b5373b1e944bb5669aabd47b7fcbccaeb8242214

See more details on using hashes here.

Provenance

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