Package: postgresql-interface
PostgreSQLInterfaceIntroductionThis 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 dueExtension to other vendors is easily achievable.StructureThe 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.GCPInstantiate the classTo 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 DatabaseTo 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 dataTo insert data into a table:db_conn.insert_table('test.simple', to_insert.copy())Update a tableTo update data into a table:db_conn.update_table('test.simple', to_update, ['id', 'date'])Delete from tableTo delete from a table:db_conn.delete_from_table('test.simple', to_delete)Execute a general statementTo execute a general SQL statement you can use the methodexecute. 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 proceduredb_conn.execute("EXECUTE test.sp_test1 @input = '%s" @ input)HerokuInstantiate the classTo 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_urlcan be found on the sectionConfig Varsinside the tabSettingsof your Heroku app or on the
sectionDatabase Credentialsof the tabSettingsof your Heroku Datastore.Read data from DatabaseTo 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 dataTo insert data into a table:db_conn.insert_table('test.simple', to_insert.copy())Update a tableTo update data into a table:db_conn.update_table('test.simple', to_update, ['id', 'date'])Delete from tableTo delete from a table:db_conn.delete_from_table('test.simple', to_delete)Execute a general statementTo execute a general SQL statement you can use the methodexecute. 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 proceduredb_conn.execute("EXECUTE test.sp_test1 @input = '%s" @ input)TestsTo 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.