How to Set Up PostgreSQL with Docker: A Developer-Friendly Guide

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 container
  • docker start dev-postgres - to restart it
  • docker 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.


Docker


Comments

Popular Posts

Install Ollama and Open WebUI on Ubuntu 24.04 with NVIDIA GPU Acceleration (Step-by-Step)

Install Ollama + Open WebUI on Ubuntu 24.04 with NVIDIA GPU Acceleration (Step-by-Step)

Install a Local AI Chatbot on Ubuntu 24.04 with Ollama and Open WebUI (Step-by-Step)

Trending Now

Recovering from Btrfs Boot Failures Using GUI Tools on Fedora

By the end of this guide the reader will be able to identify a Btrfs‑based Fedora installation, boot from a live USB, list and restore snapshots using the graphical utilities btrfs‑assistant and snapper, and verify that the system returns to a functional state without resorting to the command line. Understanding the Btrfs Layout Used by Fedora Fedora Workstation and Fedora KDE install the root filesystem as a single Btrfs partition that contains two default sub‑volumes. One sub‑volume holds the traditional “/” hierarchy, while the second is dedicated to /var/lib/machines . The latter exists to keep container images out of snapshot operations; it remains empty on systems that do not run virtual machines. Because Btrfs stores data in sub‑volumes rather than separate partitions, a snapshot captures the state of an entire sub‑volume at a point in time. The installer (Anaconda) automatically registers these sub‑volumes with the snapper service. Snapper maintains a series of read‑only ...