DEV Community

arafatruetbd
arafatruetbd

Posted on

How to Export a PostgreSQL Database from a Docker Container (pg_dump Guide)

If you're working with PostgreSQL inside Docker, sooner or later you'll need a backup.

Maybe you're about to change something risky.
Maybe you want to move your database somewhere else.
Or maybe you just don’t want to lose your data (we’ve all been there 😅).

The good news? It’s actually much simpler than it looks.

In this guide, I’ll show you the easiest way to export your PostgreSQL database from a Docker container using pg_dump.


🚀 The One-Liner (Quick Answer)

docker exec -i postgres_container pg_dump -U postgres my_database > dump.sql
Enter fullscreen mode Exit fullscreen mode

That’s it.

Run this, and you’ll get a file called dump.sql on your machine containing your entire database.


🧠 What’s Really Happening Here?

At first glance, this command looks a bit intimidating. But it’s actually doing something very logical.

  • docker exec → “Run this command inside my container”
  • -i → “Keep the connection open so data can stream out”
  • postgres_container → Your Docker container name
  • pg_dump → PostgreSQL’s built-in backup tool
  • my_database → Your database name
  • > dump.sql → “Save whatever comes out into a file on my computer”

👉 In plain English:

You’re telling Docker:
“Run pg_dump inside the container and send the result directly to my local file.”

No need to manually enter the container. No extra steps.


📦 What Do You Get in dump.sql?

The file you get is a full logical backup of your database. It includes:

  • All tables (structure)
  • All data (INSERT statements)
  • Indexes, constraints, relationships

Basically, everything you need to rebuild the database later.


✅ Why This Approach Is So Useful

I like this method because it’s:

  • ✔ Simple — just one command
  • ✔ Fast — no unnecessary steps
  • ✔ Practical — works in almost any Docker setup
  • ✔ Flexible — perfect for backups, migrations, and debugging

⚠️ Common Issues (And Quick Fixes)

❌ Authentication error

If your database uses a password, you might see an error.

Just pass it like this:

docker exec -e PGPASSWORD='your_password' -i postgres_container \
pg_dump -U postgres my_database > dump.sql
Enter fullscreen mode Exit fullscreen mode

❌ Container not running

Make sure your container is up:

docker ps
Enter fullscreen mode Exit fullscreen mode

❌ Wrong container name

Double-check the name:

docker ps --format "table {{.Names}}\t{{.Image}}"
Enter fullscreen mode Exit fullscreen mode

💡 Pro Tip: Compress the Backup

If your database is large, you probably don’t want a huge .sql file.

You can compress it on the fly:

docker exec -i postgres_container pg_dump -U postgres my_database | gzip > dump.sql.gz
Enter fullscreen mode Exit fullscreen mode

This saves space and is great for uploads or CI/CD pipelines.


🔄 How to Restore the Backup

When you need your data back:

psql -U postgres -d my_database < dump.sql
Enter fullscreen mode Exit fullscreen mode

Simple and reliable.


🧩 When You’ll Actually Use This

This command becomes super handy when you:

  • Need a quick local backup
  • Are moving data between environments
  • Want to debug production issues locally
  • Plan to make risky schema changes

Top comments (0)