This Python script connects to a PostgreSQL database, queries data based on provided names, and exports the fetched data to a JSON file.
The fetch_data.py script connects to a PostgreSQL database, retrieves data based on a list of names provided in a names.txt file, and exports the fetched data to a JSON file named data.json.
Python 3.xpsycopg2library (for PostgreSQL database connection)
To install the required dependencies, run:
pip install psycopg2
It's recommended to use a virtual environment to manage dependencies and isolate the project environment:
-
Install virtualenv if you haven't already:
pip install virtualenv -
Create a new virtual environment:
virtualenv venv -
Activate the virtual environment:
On Windows:
venv\Scripts\activate
On macOS and Linux:
source venv/bin/activate
Run the script:
python fetch_data.py
The script connects to the database, fetches data based on the names provided in names.txt, and exports the fetched data to data.json.
-
Modify the database connection parameters (
dbname,user,password,host) in the script according to your PostgreSQL database configuration. -
Customize the input source for names by uncommenting the relevant section in the script (reading from a CSV file or directly from code).
-
Ensure proper permissions to read from names.txt and write to data.json.
-
Handle errors that may occur during database connection, query execution, or file operations.