This Python script connects to a PostgreSQL database, fetches data from a specified table, and exports the data to a CSV file. Additionally, it saves images stored as base64 strings in the database to individual image files.
- Python 3.10 or higher
- PostgreSQL database with the required table
- Python packages listed in
requirements.txt
-
Clone the repository:
git clone https://github.com/yourusername/your-repository.git cd your-repository
-
Install dependencies:
pip install -r requirements.txt
-
Rename the project file
.env.example
to.env
and set the following environment variables:POSTGRES_DB=your_database_name POSTGRES_USER=your_database_user POSTGRES_PASSWORD=your_database_password POSTGRES_HOST=your_database_host POSTGRES_PORT=your_database_port PGADMIN_DEFAULT_EMAIL=xxx@example.com PGADMIN_DEFAULT_PASSWORD=xxx
-
Run the script:
python main.py
This will connect to the PostgreSQL database, fetch data from the specified table, and export it to a CSV file (
output.csv
) in the project root. Image files will be saved in theimages
directory. -
Customize SQL Query (Optional):
Edit the SQL query in the
DataProcessor
class withinmain.py
to fetch data based on your specific requirements:# Customize your SQL query here cur.execute("SELECT * FROM your_schema.your_table")
Replace
your_schema
andyour_table
with your specific schema and table names. -
View the Output:
The exported CSV file (
output.csv
) will contain the selected data columns. Image files will be saved in theimages
directory with filenames generated based on the first two columns of the fetched data.
This project is licensed under the MIT License - see the LICENSE file for details.