Exporting PostgreSQL Data to Excel with CyberOak
Data teams frequently need to extract information from PostgreSQL databases and deliver it as Excel reports for finance, operations, compliance, or management. While traditional Python projects require installing dependencies, configuring environments, and managing scheduled jobs, CyberOak takes care of all of that for you.
In this guide, you'll learn how to connect to a PostgreSQL database, execute a SQL query, and export the results to an Excel spreadsheet using a CyberOak automation.
Why CyberOak?
CyberOak provides a managed Python execution environment with all the commonly used data processing libraries already installed.
Some of the built-in libraries include:
psycopg2– Connect to PostgreSQL databases.pandas– Read and manipulate tabular data.openpyxl– Generate Excel (.xlsx) workbooks.
There are no additional Python packages to install. Simply create your automation, upload your Python script, configure your parameters, and execute.
Configuring Database Parameters
Instead of hardcoding your database credentials inside your Python script, CyberOak allows you to define them in the Key-Value Parameters section of your automation.
For example:
| Parameter | Description |
|-----------|-------------|
| DB_HOST | PostgreSQL server hostname |
| DB_PORT | Database port |
| DB_NAME | Database name |
| DB_USER | Database username |
| DB_PASSWORD | Database password |
Once these parameters have been created, CyberOak automatically makes them available to your automation during execution. This keeps sensitive information separate from your source code while allowing the same automation to be deployed across multiple environments.
Configuring the Execution Schedule
After testing your automation, simply select the desired execution frequency from the Execution Frequency dropdown.
CyberOak supports schedules such as:
- Every few minutes
- Hourly
- Daily
- Weekly
- Monthly
No cron jobs or external schedulers are required.
Breaking Down the Code
1. Secure Configuration
Rather than embedding credentials directly into the script, CyberOak stores them as Key-Value Parameters. This keeps sensitive information out of your source code and allows the same automation to be reused with different databases by simply changing the parameter values.
2. Connecting to PostgreSQL
Using psycopg2.connect(**DB_PARAMS) establishes a secure connection to the PostgreSQL server using the parameters supplied by CyberOak.
3. Reading Data with pd.read_sql_query()
Instead of manually iterating through database cursors, pd.read_sql_query() executes the SQL statement and loads the results directly into a Pandas DataFrame. Pandas automatically converts PostgreSQL data types into their Python equivalents.
4. Exporting to Excel
The to_excel() method writes the DataFrame directly to an Excel workbook. Setting index=False prevents Pandas from writing its default row index (0, 1, 2, ...) into the spreadsheet, resulting in a cleaner report.
5. Cleaning Up Resources
Always close your database connection once processing has completed. Doing so releases resources on both the PostgreSQL server and the CyberOak worker, ensuring your automation remains efficient.
Best Practices
Process Large Datasets Efficiently
If your SQL query returns millions of records, consider using the chunksize parameter in pd.read_sql_query() to process and export the data in smaller batches.
Optimize Your SQL
Perform filtering, sorting, and aggregation within PostgreSQL whenever possible using clauses such as WHERE, GROUP BY, and ORDER BY. Databases are optimized for these operations and can significantly reduce execution time.
Reuse Your Automation
Keep your Python script generic and rely on CyberOak's Key-Value Parameters for environment-specific configuration. This allows a single automation to be reused across development, staging, and production environments without modifying the code.
Happy automating with CyberOak! 🚀