This project demonstrates two approaches to export large datasets from PostgreSQL to Excel using Node.js:
- Streaming Approach (
index.js): Usespg-query-streamand ExcelJS streaming API for memory-efficient processing of large datasets. - Batch Processing Approach (
index2.js): Uses traditional batch processing with ExcelJS for simpler implementation.
The project works with a locations table containing 1 million rows with the following structure:
| Column | Type | Description |
|---|---|---|
| id | BIGSERIAL | Primary key |
| name | VARCHAR(100) | Location name |
| latitude | DECIMAL(10,8) | Geographic latitude coordinate |
| longitude | DECIMAL(11,8) | Geographic longitude coordinate |
| population | INTEGER | Population count |
| created_at | TIMESTAMPTZ | Record creation timestamp |
- Docker and Docker Compose
- Node.js 16+
- npm or yarn
-
Clone the repository:
git clone https://github.com/yourusername/node-streams.git cd node-streams -
Install dependencies:
npm install
-
Start the PostgreSQL container with sample data:
docker-compose up -d
This will:
- Start PostgreSQL 15
- Create the
locationstable - Seed it with 1 million sample records
node index.jsnode index2.jsBoth scripts will:
- Connect to the PostgreSQL database
- Export all records to an Excel/CSV file
- Show progress in the console
- Save the output file in the project root with a timestamp
Edit the following files as needed:
docker-compose.yml: Database connection settingsindex.js/index2.js: Export parameters and query customization
- Streaming Approach: Processes data in chunks (10,000 rows at a time) using Node.js streams
- Batch Processing: Loads data in batches (50,000 rows at a time) into memory
- Memory Usage: Streaming approach uses significantly less memory for large datasets
You can adjust the following settings in index.js:
batchSize: Number of records to process in each batch (default: 100,000)- Database connection settings in
dbConfig
- Increase
batchSizefor faster processing (if you have enough RAM) - Decrease
batchSizeif you encounter memory issues - The script includes progress logging every 10,000 records
To stop and remove all containers and volumes:
docker-compose down -v💡 Tip: For very large exports, consider running the script with increased Node.js memory limit:
node --max-old-space-size=4096 index.js