Skip to content

Commit 64cd79f

Browse files
committed
postgres on Docker
1 parent 3c79708 commit 64cd79f

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

content/blog/general/postgres.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
title: "Run PostgreSQL 16 with Docker Compose"
3+
author: "PrashantUnity"
4+
weight: 103
5+
date: 2026-03-24
6+
lastmod: 2026-03-24
7+
dateString: March 2026
8+
description: "Set up PostgreSQL in Docker using the official image, a named volume for persistence, and Docker Compose. Includes run, verify, connect, and cleanup commands."
9+
#canonicalURL: "https://canonical.url/to/page"
10+
cover:
11+
# image: "postgres.png" # image path/url
12+
alt: "PostgreSQL database" # alt text
13+
#caption: "PostgreSQL on Docker" display caption under cover
14+
15+
tags: [ "PostgreSQL", "Docker", "Docker Compose", "database", "development", "codefrydev" ]
16+
keywords: [ "PostgreSQL", "Docker", "Docker Compose", "postgres", "container", "codefrydev", "CFD", "local development" ]
17+
---
18+
19+
## Overview
20+
21+
This guide shows how to run [PostgreSQL](https://www.postgresql.org/) 16 locally using the official Docker image and Docker Compose. You get a default database user and database, port `5432` on your machine mapped into the container, and a **named volume** so data survives container restarts.
22+
23+
## Prerequisites
24+
25+
- **Docker Engine** installed ([Get Docker](https://docs.docker.com/get-docker/)).
26+
- **Docker Compose** v2 (bundled with Docker Desktop; on Linux you may use the `docker compose` plugin).
27+
28+
The `version:` key at the top of a Compose file is optional in modern Compose; including `version: '3.8'` is still fine for compatibility.
29+
30+
## Create `docker-compose.yml`
31+
32+
Create a file named `docker-compose.yml` in your project directory:
33+
34+
```yaml
35+
version: '3.8'
36+
37+
services:
38+
# Database service
39+
db:
40+
image: postgres:16
41+
container_name: postgres_db
42+
restart: always
43+
44+
environment:
45+
POSTGRES_USER: my_user
46+
POSTGRES_PASSWORD: my_super_secret_password
47+
POSTGRES_DB: my_database
48+
49+
ports:
50+
- "5432:5432"
51+
52+
volumes:
53+
- postgres_data:/var/lib/postgresql/data
54+
55+
volumes:
56+
postgres_data:
57+
```
58+
59+
What this does:
60+
61+
- **`image: postgres:16`** — Official PostgreSQL 16 image.
62+
- **`restart: always`** — Restarts the container if it exits or after a host reboot (when Docker starts).
63+
- **`environment`** — Creates the default role and database on first run.
64+
- **`ports`** — Maps host `5432` to the container. If something else already uses `5432` on your machine, change the left side (e.g. `"5433:5432"`) and connect to that host port instead.
65+
- **`volumes`** — `postgres_data` is a Docker named volume; it stores data at `/var/lib/postgresql/data` **inside** the container (the correct path for the official image).
66+
67+
**Security:** For real projects, avoid committing real passwords in shared repositories. Prefer environment files (e.g. `.env` not committed) with `env_file:` or your orchestrator’s secrets, and use strong passwords.
68+
69+
## Run and verify
70+
71+
From the directory that contains `docker-compose.yml`:
72+
73+
```bash
74+
docker compose up -d
75+
docker compose ps
76+
```
77+
78+
Check logs if needed:
79+
80+
```bash
81+
docker logs postgres_db
82+
```
83+
84+
You should see PostgreSQL ready to accept connections once startup finishes.
85+
86+
## Connect
87+
88+
From the host, using the same user, password, database, and **host port** you configured (default `5432`):
89+
90+
```bash
91+
psql "postgresql://my_user:my_super_secret_password@localhost:5432/my_database"
92+
```
93+
94+
### DBeaver
95+
96+
1. Open **DBeaver** and choose **Database → New Database Connection** (or click **New Connection** in the toolbar).
97+
2. Select **PostgreSQL** and click **Next**.
98+
3. On the **Main** tab, set:
99+
- **Host:** `localhost`
100+
- **Port:** `5432` (or the host port you mapped in `docker-compose.yml`, e.g. `5433` if you used `"5433:5432"`)
101+
- **Database:** `my_database`
102+
- **Username:** `my_user`
103+
- **Password:** `my_super_secret_password` (check **Save password** if you want it stored locally)
104+
4. Click **Test Connection**. If prompted, let DBeaver **download** the PostgreSQL JDBC driver the first time.
105+
5. Click **Finish** to save the connection.
106+
107+
For a typical local Docker setup, leave **SSL** off (or **disable**) on the **SSL** tab unless you have configured TLS yourself.
108+
109+
### Other GUI tools
110+
111+
Use the same values as above:
112+
113+
- **Host:** `localhost`
114+
- **Port:** `5432` (or the host port you mapped)
115+
- **Database:** `my_database`
116+
- **User / password:** as in `POSTGRES_USER` / `POSTGRES_PASSWORD`
117+
118+
Connection URI form:
119+
120+
```text
121+
postgresql://my_user:my_super_secret_password@localhost:5432/my_database
122+
```
123+
124+
## Useful commands
125+
126+
Stop containers (keeps the named volume and data):
127+
128+
```bash
129+
docker compose stop
130+
```
131+
132+
Stop and remove containers (volume **still** keeps data unless you remove it):
133+
134+
```bash
135+
docker compose down
136+
```
137+
138+
Remove the named volume and **delete persisted data** (use only when you intend to wipe the database):
139+
140+
```bash
141+
docker compose down -v
142+
```
143+
144+
List volumes:
145+
146+
```bash
147+
docker volume ls
148+
```
149+
150+
## Summary
151+
152+
You now have PostgreSQL 16 running in Docker with persistent storage via `postgres_data`, accessible on `localhost` at the mapped port. Adjust credentials and ports for your environment, and keep secrets out of version control for anything beyond local experimentation.

0 commit comments

Comments
 (0)