Skip to main content

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/.

Release files for pypsql 1.0.6

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

Source distribution (sdist)

Source distribution for pypsql 1.0.6
File Size Uploaded
pypsql-1.0.6.tar.gz 11.6 kB Details

Built distribution (wheel)

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

Total release size: 21.8 kB

Release files / pypsql-1.0.6.tar.gz

Download URL pypsql-1.0.6.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0fcd766a0ed6819578f539f3dc1d33297cbc7e35c1170217be16e476f236ee6e
BLAKE2b-256 checksum
How to use checksums
60bcf5d071e6f320795e86c72087aaf3129ded30fca86907f58176d341418739
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / pypsql-1.0.6-py3-none-any.whl

Download URL pypsql-1.0.6-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7032a165c239d61beaea0a2ec13bbf6baaf5a5671e330f24fcf6a3a5e666e8c3
BLAKE2b-256 checksum
How to use checksums
b469cbaa2cb4ddddbf766858a4f5cbebc042c32425a772438ae7882c0de6bd22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15
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