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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for postgresql-interface-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4917c5f2a1c34d9dddab64e1dfb528c26e60d017711c3a9c56002b498949e01f |
|
MD5 | 9ff6b0780412d31df6c74ab39444d140 |
|
BLAKE2b-256 | 1dafb143671fcb6645a78a6379b66bba66d0f503f902b22796406ba5c99c7e6e |
Hashes for postgresql_interface-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc02ff1a16d9d7f12a8d412c185f661cf547959f51406151e37be95cc975c13e |
|
MD5 | c2d12256d14a3625469e870b827cfea8 |
|
BLAKE2b-256 | 9843d29c4a50150d27034d29b25aa01f94026e52e4267b4b4220283efceede54 |