Skip to main content

A simple package for interacting with a Postgres Database.

Project description

Postgreasy

This package contains some simple helper functions to interact with a postgress database. Makes use of a .env file to make the connection with the database. It's basically a simple wrapper around the psycopg2 library.

Fill in the following 4 parameters in the .env file in the root folder like so:

postgres_host = ...
postgres_username = ...
postgres_database = ...
postgres_password = ...

Methods

Create a database

import postgreasy

postgreasy.create_database(database_name='example_db')

Create a schema

import postgreasy

postgreasy.create_schema(database_name='example_db')

Create a table

import postgreasy
from psycopg2 import sql

postgreasy.create_table('example_schema', 'example_table', sql.SQL('id serial, temperature int, name text'))

Execute a query

import postgreasy
from psycopg2 import sql

postgreasy.execute(sql.SQL('...'))

Fetch with a query (Execute a query and get the results)

import postgreasy
from psycopg2 import sql

x = postgreasy.execute(sql.SQL('select 1'))

Insert a Pandas DataFrame

import postgreasy
import pandas as pd

df = pd.DataFrame({'x': [1, 2, 3], 'y': [5, 28, 8]})
postgreasy.insert_df(df, 'example_schema', 'example_table')

Connection

Normally when you execute one of the postgreasy methods, a connection is automatically established and closed. For example:

import postgreasy
from psycopg2 import sql

postgreasy.execute_query_on_db(sql.SQL('select 1'))

To create a connection without the .env or if you want to execute multiple queries and not create a connection for each separate query there are 2 ways to go about it. Either use the get_connection() method or use the with ... structure with a PostgresConnection() object. The latter is the preferred option as it requires less steps that can be forgotten.

get_connection()

This way you create a connection object which you supply to the other postgreasy methods. Here you have to remember to manually close the connection with the .close() function.

Example:

import postgreasy
from psycopg2 import sql

conn = postgreasy.get_connection(host='...', database='...', username='...', password='...')
postgreasy.execute_query_on_db(sql.SQL('select 1'), conn )
conn.close()

PostgresConnection()

In this case the connection is automatically opened, you also don't have to supply the connection paramter to the postgreasy methods.

Example:

import postgreasy
from psycopg2 import sql

with PostgresConnection(host='...', database='...', username='...', password='...') as conn:
    conn.execute_query_on_db(sql.SQL('select 1') )

Contact

Go to the GitHub page if you hav any issues.

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

postgreasy-1.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

postgreasy-1.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file postgreasy-1.0.0.tar.gz.

File metadata

  • Download URL: postgreasy-1.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.10

File hashes

Hashes for postgreasy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 24cf657bf587a98cb4df69f8d3d2a2cb1dd9f20dc6758f3dc573418f971e77e4
MD5 3dfa289bbd4c56f293d6262131ca60c3
BLAKE2b-256 db1c465871c58c0282522c84e88d5c24b27e67a444e9ea8c61f98ef160cba80f

See more details on using hashes here.

File details

Details for the file postgreasy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: postgreasy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.10

File hashes

Hashes for postgreasy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b66af376241fe32d91facf85855052b0570870f9b57f07e6f4ada3fb3f7c9b69
MD5 6bec8fbda8fd1a8e29146d82e8a3bf24
BLAKE2b-256 b0d309a9ebaef246a7586f5c241a406092facbdad42a326a4fc011bf4cdb4e8d

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