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 yo...

Source: DEV Community
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 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_data