Skip to main content

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.

Metadata

Release files for postgresql-interface 1.0.2

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

Source distribution (sdist)

Source distribution for postgresql-interface 1.0.2
File Size Uploaded
postgresql-interface-1.0.2.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for postgresql-interface 1.0.2
File Interpreter ABI Platform
postgresql_interface-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / postgresql-interface-1.0.2.tar.gz

Download URL postgresql-interface-1.0.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4917c5f2a1c34d9dddab64e1dfb528c26e60d017711c3a9c56002b498949e01f
BLAKE2b-256 checksum
How to use checksums
1dafb143671fcb6645a78a6379b66bba66d0f503f902b22796406ba5c99c7e6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.14

Release files / postgresql_interface-1.0.2-py3-none-any.whl

Download URL postgresql_interface-1.0.2-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc02ff1a16d9d7f12a8d412c185f661cf547959f51406151e37be95cc975c13e
BLAKE2b-256 checksum
How to use checksums
9843d29c4a50150d27034d29b25aa01f94026e52e4267b4b4220283efceede54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.14

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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