If you’re running PostgreSQL inside a Docker container, importing a SQL dump file is a common task when setting up or migrating databases. In this quick guide, I’ll show you how to copy your SQL file into the container and import it easily.
Prerequisites
- A running PostgreSQL container (e.g., named
demo_db) - Your SQL dump file ready on your host machine (e.g.,
demo.sql) - Basic knowledge of Docker commands
Step 1: Copy the SQL file into the container
Use the docker cp command to copy the SQL file from your local machine into the PostgreSQL container:
docker cp demo.sql demo_db:/demo.sql
This copies the file to the root directory / inside the container.
Step 2: Import the SQL file into the database
Run the psql command inside the container to execute the SQL file against your target database:
docker exec -i demo_db psql -U postgres -d demo_database -f /demo.sql
-
demo_dbis the container name. -
postgresis the PostgreSQL user. -
demo_databaseis your database name. -
-f /demo.sqlpoints to the SQL file inside the container.
Troubleshooting tips
- Ensure your container is running (
docker ps). - Verify database name and user credentials.
- Large files might take time, so be patient.
Conclusion
Importing SQL files into a Dockerized PostgreSQL database is straightforward once you get familiar with the commands. Using docker cp and docker exec, you can easily manage your database dumps without leaving your containerized environment.
If you found this helpful, feel free to clap and follow for more Docker and database tips!
Top comments (0)