- class pipableai.core.postgresql_connector.PostgresConfig(host: str, port: int, database: str, user: str, password: str)
Bases:
object
Data class for PostgreSQL connection configuration.
- Args:
host (str): The hostname or IP address of the PostgreSQL server. port (int): The port number to connect to on the PostgreSQL server. database (str): The name of the PostgreSQL database. user (str): The username for connecting to the PostgreSQL server. password (str): The password for the specified username.
- Attributes:
host (str): The hostname or IP address of the PostgreSQL server. port (int): The port number to connect to on the PostgreSQL server. database (str): The name of the PostgreSQL database. user (str): The username for connecting to the PostgreSQL server. password (str): The password for the specified username.
- database: str
- host: str
- password: str
- port: int
- user: str
- class pipableai.core.postgresql_connector.PostgresConnector(config: PostgresConfig)
Bases:
DatabaseConnectorInterface
A class for establishing and managing the PostgreSQL database connection.
This class provides methods for connecting to a remote PostgreSQL server and executing SQL queries. It uses the psycopg2 library for database interaction.
- Args:
config (PostgresConfig): The configuration for connecting to the PostgreSQL server.
- Attributes:
config (PostgresConfig): The configuration for connecting to the PostgreSQL server. connection (psycopg2.extensions.connection): The connection to the PostgreSQL server. cursor (psycopg2.extensions.cursor): The cursor for executing SQL queries.
- Raises:
ConnectionError: If failed to connect to the PostgreSQL server. ValueError: If an error occurs during query execution.
- Note:
The execute_query method returns the query results as a Pandas DataFrame.
- Warning:
Ensure to disconnect from the database using the disconnect method after executing queries to release resources.
- Example:
To establish a connection and execute a query, create an instance of PostgresConnector and call the execute_query method.
from pipable.connector import PostgresConnector, PostgresConfig # Define PostgreSQL configuration postgres_config = PostgresConfig( host="your_postgres_host", port=5432, # Replace with your port number database="your_database_name", user="your_username", password="your_password", ) # Initialize the PostgresConnector instance connector = PostgresConnector(postgres_config) # Execute a SQL query result = connector.execute_query("SELECT * FROM Employees")
- connect()
Establish a connection to the PostgreSQL server.
- disconnect()
Close the connection to the PostgreSQL server.
- execute_query(query: str) DataFrame
Execute an SQL query on the connected PostgreSQL server and return the result as a Pandas DataFrame.
- Args:
query (str): The SQL query to execute.
- Returns:
DataFrame: A Pandas DataFrame representing the query results.
- Raises:
ValueError: If an error occurs during query execution.