Skip to main content

Functionality to connect and interact with PostgreSQL servers

Project description

pypSQL

pypsql utilities website

License: MIT

pypsql is a lightweight framework for connecting to PostgreSQL databases, whether locally or remotely hosted. It builds on top of SQLAlchemy to provide a simple, Pythonic interface for establishing connections, running queries, and managing database sessions.

Contributors

Marius Liebald (maintainer)

Installation

pip install pypsql

Usage

Local Usage

Provide database credentials

You can store the database connection details in a file, for example .env:

SERVER=<host name or address>   # e.g., localhost, 127.0.0.1, or a public IP
PORT=<port number>              # default: 5432
NAME_DATABASE=<database name>   # e.g., my_db
NAME_USER=<role>                # e.g., Alice
PASSWORD_USER=<password>        # e.g., 123abc

You may place this file anywhere on the client machine. Importantly, this step is optional. You can also provide the variables interactively via terminal prompts.

When to be used?

Use this approach if an SSH tunnel is not needed. This is the case if:

  • the server and client run on the same machine,
  • the server and client are in the same (virtual) network, or
  • the server is accessible via its public IP.

Connecting

In this case, simply run the Python script:

import pypsql
from pathlib import Path

sql_script = """
SELECT *
FROM customers
"""

# local connection
conn = pypsql.DatabaseConnector(
    db_credential_file='.env_local'
)

df = conn.get_data(sql_script)
print(df)

Usage via an SSH Tunnel

Provide database & SSH credentials

Again, create an .env file. You may want to store it in the same directory as the Python script that accesses the database when executed. This time the .env needs to follow this structure:

SERVER=<host name or address>              # e.g., localhost, 127.0.0.1
PORT=<port number>                         # default: 5432
NAME_DATABASE=<database name>              # e.g., my_db
NAME_USER=<role>                           # e.g., Alice
PASSWORD_USER=<password>                   # e.g., 123abc
SSH_USERNAME=<public IP of remote server>
SSH_PKEY=<path of private SSH key>         # e.g., ~/.ssh/id_rsa
SSH_PORT=<ssh port of remote server>       # default: 22

Connecting

Then you can connect to the database via the SSH tunnel in the following way:

from pypsql import SSHDatabaseConnector
from pathlib import Path

sql_script = """
SELECT *
FROM customers
"""

with SSHDatabaseConnector(
  ssh_port = 22,
  db_credential_file='.env_ssh'
) as ssh_db:    
  df = ssh_db.get_data(sql_script)
  print(df)

Official Documentation

The documentation is hosted under https://www.marius-liebald.com/pypsql/index.html

License

The package is distributed under the MIT license.

References

Bayer, Michael, and contributors. 2025. “SQLAlchemy.” https://www.sqlalchemy.org/.

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

pypsql-1.0.4.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

pypsql-1.0.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file pypsql-1.0.4.tar.gz.

File metadata

  • Download URL: pypsql-1.0.4.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pypsql-1.0.4.tar.gz
Algorithm Hash digest
SHA256 0cfc3aec5903c98627f8e31856e5e7bffec70bc0d9903e6dc2f5609dca18b14f
MD5 92dfa165ce2b9ec1e35e1e9fa0f31f43
BLAKE2b-256 25052778521b3e99f10ba24e589a63f210b31c81a1d2b5472ce35338e5fe07b8

See more details on using hashes here.

File details

Details for the file pypsql-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: pypsql-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for pypsql-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 182def67afceb004136dc0fd50aa8742a4ad2a3a7e1f9e43b9667968af057020
MD5 15c81d81a212dfd8f363c6e46b354b84
BLAKE2b-256 4d3aec89ffcfb42ac2a5373914bb365b055e7e7d27cf43b57ec5482827fc6bf5

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