How to export data from a postgres container inside of Docker

James Phoenix
James Phoenix

If you are running a postgres database inside of Docker, it can be a powerful tool for quickly and easily transferring data. Exporting data from a postgres container inside of Docker is a relatively straightforward process and can be done with a few simple command line steps. We’ll walk through the steps for how to properly export data from a postgres container inside of Docker.

Unleash Your Potential with AI-Powered Prompt Engineering!

Dive into our comprehensive Udemy course and learn to craft compelling, AI-optimized prompts. Boost your skills and open up new possibilities with ChatGPT and Prompt Engineering.

Embark on Your AI Journey Now!
docker exec -it -u database_user_name container_name \
psql -d database_name -c "COPY (SELECT * FROM table) TO STDOUT CSV" > output.csv
docker exec -it -u postgres postgres \
psql -d TEST_DB -c "COPY (SELECT * FROM tenants) TO STDOUT WITH CSV HEADER" > output.csv

Now let’s assume you’ve got multiple tables that you want to output:

#!/bin/bash 

# declare an array of table names 
declare -a tables=("table1" "table2" "table3")

# loop over the array of table names
for table in "${tables[@]}"
do
  # execute the psql command for each table
  docker exec -it -u database_user_name container_name \
  psql -d database_name -c "COPY (SELECT * FROM $table) TO STDOUT CSV" > "$table"_output.csv

done

Exporting data from a postgres container inside of Docker can be a complex process, but by following the steps outlined in this article, it can be done with relative ease. It is important to remember to use the right credentials and access rights to ensure the data is properly exported and secured.

Additionally, taking the time to understand the principles of docker containers and postgres databases can help make the process easier to understand. With the right knowledge and guidance, exporting data from a postgres container inside of Docker can be a successful and straightforward task.


More Stories

Cover Image for Why I’m Betting on AI Agents as the Future of Work

Why I’m Betting on AI Agents as the Future of Work

I’ve been spending a lot of time with Devin lately, and I’ve got to tell you – we’re thinking about AI agents all wrong. You and I are standing at the edge of a fundamental shift in how we work with AI. These aren’t just tools anymore; they’re becoming more like background workers in our digital lives. Let me share what I’ve…

James Phoenix
James Phoenix
Cover Image for Supercharging Devin + Supabase: Fixing Docker Performance on EC2 with overlay2

Supercharging Devin + Supabase: Fixing Docker Performance on EC2 with overlay2

The Problem While setting up Devin (a coding assistant) with Supabase CLI on an EC2 instance, I encountered significant performance issues. After investigation, I discovered that Docker was using the VFS storage driver, which is known for being significantly slower than other storage drivers like overlay2. The root cause was interesting: the EC2 instance was already using overlayfs for its root filesystem,…

James Phoenix
James Phoenix