PostQL

Methods Description

Creating a PostgreSQL Instance


Creating a PostgreSQL Instance

Before you can perform any database management or data operations, you need to create an instance of the Postgres class. This instance represents a connection to a PostgreSQL database.

To create a PostgreSQL instance, you need to provide the connection details such as the host, port, user, and password.

db = Postgres( host="localhost", port="5432", user="my_user", password="my_password")

Database Management Methods

connect(database)

The connect method establishes a connection to the PostgreSQL server.

Parameters

Returns

Example

db.connect(database="my_database")

Error Handling

If an error occurs during the connection process, such as an authentication failure or a database connection issue, the method catches the psycopg2.Error exception, logs an error message, and re-raises the exception.

Use Case

Connecting to a database is a fundamental operation when working with PostgreSQL. This method is used to establish a connection to a database before performing any database management or data operations.

disconnect()

The disconnect method closes the current database connection.

Parameters

Returns

Example

db.disconnect()

Error Handling

The method does not explicitly handle errors during the disconnection process, as the psycopg2 library’s close method does not typically raise exceptions for errors that occur during the disconnection process.

Use Case

Disconnecting from a database is important for freeing up resources and ensuring that the database connection is properly closed when it is no longer needed.

create_database(new_database)

The create_database method creates a new database.

Parameters

Returns

Example

db.create_database("new_database")

Error Handling

If an error occurs during the database creation process, such as a syntax error in the SQL query or a permission issue, the method logs an error message and re-raises the exception.

Use Case

Creating a new database is a common operation when setting up a new PostgreSQL environment or when creating a new database for a specific application.

delete_database(database_to_delete)

The delete_database method deletes a specified database.

Parameters

Returns

Example

db.delete_database("database_to_delete")

Error Handling

If an error occurs during the database deletion process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Deleting a database is a common operation when cleaning up old or unused databases, especially in development or testing environments.

create_user(username, password, is_superuser)

The create_user method creates a new user with the specified username and password.

Parameters

Returns

Example

db.create_user("new_user", "new_password", is_superuser=True)

Error Handling

If an error occurs during the user creation process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Creating new users is a common operation when setting up a new PostgreSQL environment or when adding new users for specific applications or services.

delete_user(username)

The delete_user method deletes a specified user.

Parameters

Returns

Example

db.delete_user("user_to_delete")

Error Handling

If an error occurs during the user deletion process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Deleting users is a common operation when cleaning up old or unused users, especially in development or testing environments.

create_table(name, schema)

The create_table method creates a new table with the specified name and schema.

Parameters

Returns

Example

db.create_table("new_table", {"id": "SERIAL PRIMARY KEY", "name": "VARCHAR(255)"})

Error Handling

If an error occurs during the table creation process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Creating new tables is a common operation when setting up a new PostgreSQL environment or when adding new tables for specific applications or services.

drop_table(name)

The drop_table method drops a specified table.

Parameters

Returns

Example

db.drop_table("table_to_drop")

Error Handling

If an error occurs during the table dropping process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Dropping tables is a common operation when cleaning up old or unused tables, especially in development or testing environments.

get_table_schema(table)

The get_table_schema method retrieves and prints the schema of a specified table.

Parameters

Returns

Example

db.get_table_schema("my_table")

Error Handling

If an error occurs during the schema retrieval process, such as a syntax error in the SQL query or a permission issue, the method logs an error message and re-raises the exception.

Use Case

Retrieving the schema of a table is useful for understanding the structure of the table and for planning database migrations or schema changes.

get_connection_details()

The get_connection_details method retrieves the connection details.

Parameters

Returns

Example

db.get_connection_details()

Error Handling

The method does not explicitly handle errors, as it simply logs the connection details.

Use Case

Retrieving connection details is useful for troubleshooting and for verifying that the correct database and user are being used.

This documentation provides a comprehensive guide to using the DatabaseManagement class for managing PostgreSQL databases and users. It includes detailed descriptions of each method, along with examples of how to use them.

Data Operations Methods

select(table, columns=None)

The select method constructs a SELECT SQL query.

Parameters

Returns

Example

db.select("my_table", ["column1", "column2"])

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Selecting data from a table is a fundamental operation when working with PostgreSQL. This method is used to construct a SELECT query, which can be further refined with additional clauses like WHERE, JOIN, etc.

insert(table, data)

The insert method constructs an INSERT SQL query.

Parameters

Returns

Example

db.insert("my_table", {"column1": "value1", "column2": "value2"})

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Inserting data into a table is a common operation when working with PostgreSQL. This method is used to construct an INSERT query.

update(table)

The update method starts the construction of an UPDATE SQL query.

Parameters

Returns

Example

db.update("my_table")

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Updating data in a table is a common operation when working with PostgreSQL. This method is used to start the construction of an UPDATE query, which can be further refined with SET and WHERE clauses.

set(data)

The set method adds a SET clause to an UPDATE SQL query.

Parameters

Returns

Example

db.update("my_table").set({"column1": "new_value1", "column2": "new_value2"})

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Setting new values for columns in an UPDATE query is a common operation when working with PostgreSQL. This method is used to add a SET clause to an UPDATE query.

delete(table)

The delete method constructs a DELETE SQL query.

Parameters

Returns

Example

db.delete("my_table")

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Deleting data from a table is a common operation when working with PostgreSQL. This method is used to construct a DELETE query, which can be further refined with a WHERE clause.

join(table, join_type="INNER")

The join method adds a JOIN clause to a SQL query.

Parameters

Returns

Example

db.join("another_table", "LEFT")

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Joining tables is a common operation when working with PostgreSQL. This method is used to add a JOIN clause to a query, which can be further refined with an ON clause.

on(condition)

The on method adds an ON clause to a JOIN SQL query.

Parameters

Returns

Example

db.on("my_table.id = another_table.id")

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Specifying the condition for a join is a common operation when working with PostgreSQL. This method is used to add an ON clause to a JOIN query.

where(conditions)

The where method adds a WHERE clause to a SQL query.

Parameters

Returns

Example

db.where({"column1": "value1", "column2": ("<", "value2")})

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Filtering rows in a query is a common operation when working with PostgreSQL. This method is used to add a WHERE clause to a query.

order(columns, asc=False)

The order method adds an ORDER BY clause to a SQL query.

Parameters

Returns

Example

db.order(["column1", "column2"], asc=True)

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Ordering rows in a query is a common operation when working with PostgreSQL. This method is used to add an ORDER BY clause to a query.

groupby(columns)

The groupby method adds a GROUP BY clause to a SQL query.

Parameters

Returns

Example

db.groupby(["column1", "column2"])

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Grouping rows in a query is a common operation when working with PostgreSQL. This method is used to add a GROUP BY clause to a query.

limit(limit_value)

The limit method adds a LIMIT clause to a SQL query.

Parameters

Returns

Example

db.limit(10)

Error Handling

This method does not explicitly handle errors. Errors in the SQL query construction or execution are handled by the execute method.

Use Case

Limiting the number of rows returned in a query is a common operation when working with PostgreSQL. This method is used to add a LIMIT clause to a query.

execute()

The execute method executes the constructed SQL query.

Parameters

Returns

Example

db.execute()

Error Handling

If an error occurs during the query execution process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Executing a constructed SQL query is a fundamental operation when working with PostgreSQL. This method is used to execute the query that has been constructed with the previous methods.

sql(query)

The sql method executes a raw SQL query.

Parameters

Returns

Example

db.sql("SELECT * FROM my_table")

Error Handling

If an error occurs during the query execution process, such as a syntax error in the SQL query or a permission issue, the method logs an error message.

Use Case

Executing raw SQL queries is useful for operations that cannot be easily constructed with the provided methods. This method is used to execute any SQL query.

to_csv(output_file)

Exports the query results to a CSV file.

Parameters
Returns
Example

db.select(table="users", columns=["name", "age"]).where({'age': ('>',25)}).order(["age"], asc=True).limit(3).to_csv("user_results.csv")

Error Handling

If an error occurs during the export process, such as a file write error, the method logs an error message.

Use Case

Exporting query results to a CSV file is useful for data analysis and reporting. This method is used to export the results of a query to a CSV file, which can then be easily imported into other tools or applications.

to_excel(output_file)

Exports the query results to an Excel file.

Parameters
Returns
Example

db.select(table="users", columns=["name", "age"]).where({'age': ('>',25)}).order(["age"], asc=True).limit(3).to_excel("user_results.xlsx")

Error Handling

If an error occurs during the export process, such as a file write error, the method logs an error message.

Use Case

Exporting query results to an Excel file is useful for data analysis and reporting. This method is used to export the results of a query to an Excel file, which can then be easily imported into other tools or applications.

to_json(output_file)

Exports the query results to a JSON file.

Parameters
Returns
Example

db.select(table="users", columns=["name", "age"]).where({'age': ('>',25)}).order(["age"], asc=True).limit(3).to_json("user_results.json")

Error Handling

If an error occurs during the export process, such as a file write error, the method logs an error message.

Use Case

Exporting query results to a JSON file is useful for data analysis and reporting. This method is used to export the results of a query to a JSON file, which can then be easily imported into other tools or applications.

to_parquet(output_file)

Exports the query results to a Parquet file.

Parameters
Returns
Example

db.select(table="users", columns=["name", "age"]).where({'age': ('>',25)}).order(["age"], asc=True).limit(3).to_parquet("user_results.parquet")

Error Handling

If an error occurs during the export process, such as a file write error, the method logs an error message.

Use Case

Exporting query results to a Parquet file is useful for data analysis and reporting. This method is used to export the results of a query to a Parquet file, which can then be easily imported into other tools or applications.

upload_to_s3(bucket_name, format, filename, acl=None, metadata=None)

Uploads the exported file to an Amazon S3 bucket.

Parameters
Returns
Example

db.select(table="users", columns=["name", "age"]).where({'age': ('>',25)}).order(["age"], asc=True).limit(3).to_parquet("user_results.parquet").upload_to_s3("my_data_bucket", "parquet", "user_results.parquet")

Error Handling

If an error occurs during the upload process, such as an S3 access error, the method logs an error message.

Use Case

Uploading exported files to Amazon S3 is useful for storing and sharing data. This method is used to upload the exported file to an S3 bucket, making it accessible for further analysis or sharing.