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.6.tar.gz (11.6 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.6-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pypsql-1.0.6.tar.gz
  • Upload date:
  • Size: 11.6 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.6.tar.gz
Algorithm Hash digest
SHA256 0fcd766a0ed6819578f539f3dc1d33297cbc7e35c1170217be16e476f236ee6e
MD5 60b332570163d40afa9cb7148d979122
BLAKE2b-256 60bcf5d071e6f320795e86c72087aaf3129ded30fca86907f58176d341418739

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pypsql-1.0.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 7032a165c239d61beaea0a2ec13bbf6baaf5a5671e330f24fcf6a3a5e666e8c3
MD5 cecfa33bc887875e264e38b17b62ad0a
BLAKE2b-256 b469cbaa2cb4ddddbf766858a4f5cbebc042c32425a772438ae7882c0de6bd22

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