Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pgsu

Package Overview
Dependencies
Maintainers
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pgsu

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

  • 0.3.0
  • PyPI
  • Socket score

Maintainers
3

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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc