A simple script to dump all tables from a PostgreSQL database into CSV files
using psql. This repository contains a batch script for Windows that
automates the process of exporting each table to a separate CSV file.
- PostgreSQL client tools (including
psql) installed on your system. - Access to a PostgreSQL database.
- Basic understanding of command-line operations.
The provided script is a Windows batch file (.bat) that:
- Prompts for PostgreSQL database connection details.
- Fetches a list of tables from the
publicschema. - Exports each table to a CSV file in the current directory.
git clone https://github.com/prave-com/psql-dump-csv
cd psql-dump-csv- Open Command Prompt: Navigate to the directory where the script is located.
- Run the Script: Execute the script by typing:
dump.bat
- Provide Connection Details: You will be prompted to enter the following
database connection details:
- Database Name: The name of the database you want to dump.
- Username: Your PostgreSQL username.
- Password: Your PostgreSQL password.
- Host: The hostname of your PostgreSQL server (default is
localhost). - Port: The port on which PostgreSQL is listening (default is
5432). - SSL Mode: SSL mode for the connection. Options are
disable,require,verify-ca,verify-full(default isprefer).
- Check Output: The script will export each table to a CSV file in the current
directory. The CSV files will be named in the format:
database_name_table_name.csv.
Enter database name: mydatabase
Enter username: myuser
Enter password: mypassword
Enter host (default is localhost): localhost
Enter port (default is 5432): 5432
Enter SSL mode (default is prefer) [options: disable, require, verify-ca, verify-full]: require
After running the script, you will find CSV files for each table in the current
directory, e.g., mydatabase_table1.csv, mydatabase_table2.csv, etc.
The script file is named dump.bat. It uses the following environment
variables:
DB_NAME: Database name.DB_USER: Username.DB_PASS: Password.DB_HOST: Hostname (default: localhost).DB_PORT: Port (default: 5432).SSL_MODE: SSL mode for connection (default: prefer).OUTPUT_DIR: Directory where CSV files will be saved which is current directory.
- Fetching Table List: Retrieves a list of tables from the
publicschema. - Exporting Tables: Uses the
\COPYcommand to export each table to a CSV file.
Feel free to open issues or submit pull requests if you have improvements or bug fixes.