Skip to main content

Library to load and extract data from a PosgreSQL Database with Python with a simple SQL style language

Project description

PostgreSQLInterface

Introduction

This package allows loading and extracting data from a PostgresSQL Database using pandas dataframes and simple SQL style methods. At the moment, only has been implemented for Heroku and GCP databases. Nonetheless, Heroku testing has been deprecated due

Extension to other vendors is easily achievable.

Structure

The library has a parent abstract class that contains the main methods, then a children class implements the particularities of a vendor, finally, a factory method handles the creation of the objects.
Below, you can find a little introduction to all relevant methods. For more information, read the doc of the method or take a look to the pytest tests in the repo.

GCP

Instantiate the class

To instantiate the class to connect to a Heroku Database:

from postgresql_interface.postgresql_interface import postgres_sql_connector_factory
db_conn = postgres_sql_connector_factory(
            vendor='gcp', host=host, database_name=database_name, 
            user_name=user_name, user_password=user_password, port=port
)

Read data from Database

To retrieve a query from the database:

my_table = db_conn.query("SELECT * FROM test.data")

It is also possible to do more complicated queries:

my_table = db_conn.query("SELECT * FROM test.data d JOIN test.references r ON d.referencesid = r.id")

Insert data

To insert data into a table:

db_conn.insert_table('test.simple', to_insert.copy())

Update a table

To update data into a table:

db_conn.update_table('test.simple', to_update, ['id', 'date'])

Delete from table

To delete from a table:

db_conn.delete_from_table('test.simple', to_delete)

Execute a general statement

To execute a general SQL statement you can use the method execute. This method returns no data. It is important to notice that it is not the SQL execute command. As an example, you can use it to create a schema:

db_conn.execute("CREATE SCHEMA test")

You can also use it to execute a stored procedure

db_conn.execute("EXECUTE test.sp_test1 @input = '%s" @ input)

Heroku

Instantiate the class

To instantiate the class to connect to a Heroku Database:

from postgresql_interface.postgresql_interface import postgres_sql_connector_factory
db_conn = postgres_sql_connector_factory(vendor='heroku', database_url=database_url)

database_url can be found on the section Config Vars inside the tab Settings of your Heroku app or on the section Database Credentials of the tab Settings of your Heroku Datastore.

Read data from Database

To retrieve a query from the database:

my_table = db_conn.query("SELECT * FROM test.data")

It is also possible to do more complicated queries:

my_table = db_conn.query("SELECT * FROM test.data d JOIN test.references r ON d.referencesid = r.id")

Insert data

To insert data into a table:

db_conn.insert_table('test.simple', to_insert.copy())

Update a table

To update data into a table:

db_conn.update_table('test.simple', to_update, ['id', 'date'])

Delete from table

To delete from a table:

db_conn.delete_from_table('test.simple', to_delete)

Execute a general statement

To execute a general SQL statement you can use the method execute. This method returns no data. It is important to notice that it is not the SQL execute command. As an example, you can use it to create a schema:

db_conn.execute("CREATE SCHEMA test")

You can also use it to execute a stored procedure

db_conn.execute("EXECUTE test.sp_test1 @input = '%s" @ input)

Tests

To be able to execute the tests, it is necessary to provide a '.env' file with the url to connect to a GCP database. Currently, Heroku testing is disabled due to change in pricing.

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

postgresql-interface-1.0.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

postgresql_interface-1.0.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file postgresql-interface-1.0.2.tar.gz.

File metadata

  • Download URL: postgresql-interface-1.0.2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for postgresql-interface-1.0.2.tar.gz
Algorithm Hash digest
SHA256 4917c5f2a1c34d9dddab64e1dfb528c26e60d017711c3a9c56002b498949e01f
MD5 9ff6b0780412d31df6c74ab39444d140
BLAKE2b-256 1dafb143671fcb6645a78a6379b66bba66d0f503f902b22796406ba5c99c7e6e

See more details on using hashes here.

File details

Details for the file postgresql_interface-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for postgresql_interface-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bc02ff1a16d9d7f12a8d412c185f661cf547959f51406151e37be95cc975c13e
MD5 c2d12256d14a3625469e870b827cfea8
BLAKE2b-256 9843d29c4a50150d27034d29b25aa01f94026e52e4267b4b4220283efceede54

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