PostgreSQL is a powerful open-source relational database, widely used in modern applications. Docker, on the other hand, simplifies environment setup and management. In this guide, we'll walk you through the steps to run PostgreSQL inside a Docker container—perfect for development or testing environments.
Step 1: Install Docker
Make sure Docker is installed and running on your machine. You can download it from Docker’s official site.
Step 2: Run PostgreSQL Container
Open your terminal and execute the following command:
docker run --name dev-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
This will pull the latest PostgreSQL image and run it on port 5432.
Step 3: Connect to the Database
You can now connect using any PostgreSQL client like DBeaver or psql:
psql -h localhost -U postgres
Step 4: Persist Data (Optional)
To ensure your data survives container restarts, use a volume:
docker run --name dev-postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pgdata:/var/lib/postgresql/data \
-d -p 5432:5432 postgres
Step 5: Manage the Container
docker stop dev-postgres
- to stop the containerdocker start dev-postgres
- to restart itdocker rm dev-postgres
- to remove it
That's it! You've successfully launched a PostgreSQL instance inside Docker. This method keeps your system clean and lets you spin up databases quickly for any project.
Comments
Post a Comment