This repository contains a minimal Docker Compose configuration to run a PostgreSQL instance locally.
Files added
docker-compose.yml— Compose file that defines adbservice using the official Postgres image, a host bind mount by default (./pgdata), and a healthcheck..env— Default environment variables (username, password, database).
Quick start (PowerShell)
-
Optionally edit
.envto change credentials. -
Start Postgres in the background:
docker compose up -d
- Check status and logs:
docker compose ps
docker compose logs -f db
- Connect with psql inside the container:
docker compose exec db psql -U $env:POSTGRES_USER -d $env:POSTGRES_DB
If you prefer a host-based psql client, use the values from .env to connect on port 5432 (for example using a GUI client or psql on the host).
Notes
- The service is configured to persist database files into the host folder
./pgdataby default (so data is directly visible in the repository folder). - To remove containers while keeping the
pgdatafolder, rundocker compose down. - If you prefer Docker-managed storage instead, switch back to the named volume by editing
docker-compose.yml(see the "Persisting data on the host" section).
Persisting data on the host (optional)
If you'd prefer the database files to be stored in a folder inside this repository (useful for simple backups or inspection), you can use a host bind mount instead of the named Docker volume.
- Create a
pgdatafolder at the repository root (PowerShell):
New-Item -ItemType Directory -Path .\pgdata
-
Allow Docker access to your drive (Docker Desktop on Windows must have the drive/shared resource enabled).
-
Edit
docker-compose.ymland switch thevolumesentry for thedbservice from the named volume:
- postgres_data:/var/lib/postgresql/data
to the host bind mount (uncomment):
# - ./pgdata:/var/lib/postgresql/data
so it becomes:
- ./pgdata:/var/lib/postgresql/data
- Restart the stack:
docker compose down
docker compose up -d
VS Code Tasks
This project includes several VS Code tasks to help manage the Postgres instance. To run a task:
- Open the Command Palette (Ctrl+Shift+P)
- Type "Tasks: Run Task"
- Select one of these tasks:
Database Control:
Start Postgres: Starts the database containerStop Postgres: Stops and removes containers (keeps the data volume)Restart Postgres: Restarts the containerClean All: Removes containers AND volume (DANGER: deletes all data)
Monitoring:
View Logs: Shows live container logs in a dedicated terminalList Volumes: Shows all Podman volumes including pgdatapsql Shell: Opens a PostgreSQL interactive shell inside the container
Backup:
Backup Volume: Exports the volume data to./backups/postgres_backup_[timestamp].tar
Additional Tasks
Restore Latest Backup (DANGER): Restores the most recent backup from./backupsinto thepgdatavolume. This task will stop the stack, remove the named volumepgdata, recreate it, extract the backup into the volume, and restart the stack. Use with caution.Create Database (appdb): RunsCREATE DATABASE appdb;inside the running container. Change the database name in the task if you prefer a different name.Show DB Sizes: Runs a query to display all databases and their sizes.
Keyboard Shortcuts (workspace)
The workspace includes a set of suggested keybindings you can enable in VS Code (see .vscode/keybindings.json) — default examples are:
- Ctrl+Alt+S — Start Postgres
- Ctrl+Alt+K — Stop Postgres
- Ctrl+Alt+R — Restart Postgres
- Ctrl+Alt+L — View Logs
- Ctrl+Alt+B — Backup Volume
- Ctrl+Alt+Shift+R — Restore Latest Backup (DANGER)
Notes
- The restore task expects a backup file named like
postgres_backup_YYYYMMDD_HHMMSS.tarinside thebackupsfolder. The task picks the most recent one. - All tasks use
podmancommands (adjust todockerif you prefer Docker Desktop). If you want Docker instead, I can add duplicate tasks that usedocker composeanddocker volumecommands.
Notes and caveats
- Data is persisted in a named Podman volume
pgdataand survives container restarts/recreation - Use
Clean Alltask with caution - it will delete all database data - Backups are stored in the
backupsdirectory with timestamps
Exposing the Postgres port for external access
By default this compose file publishes Postgres on port 5432 and is now explicitly bound to all host network interfaces so other machines on your LAN can connect.
- Ensure your Windows firewall allows inbound TCP connections on port 5432 (or the port you choose). In PowerShell (run as Administrator) you can add a rule:
New-NetFirewallRule -DisplayName "Allow Postgres 5432" -Direction Inbound -LocalPort 5432 -Protocol TCP -Action Allow
-
If you only want access from specific hosts, update
docker-compose.ymlto bind to a specific host IP instead of0.0.0.0(for example192.168.1.100:5432:5432). -
Test from another machine on the same network (replace
<HOST_IP>with the host machine IP):
# using psql on a remote machine
psql -h <HOST_IP> -p 5432 -U postgres -d postgres
- If you're using
podman composeor other runtimes, published ports are handled similarly but check the runtime docs — podman on Windows can behave differently than Docker Desktop.
Security notes
- Do not expose Postgres to the public internet without additional protections (VPN, SSH tunnel, firewall rules). If you must, use strong passwords, restrict allowed client IPs, and consider TLS/SSL.
- You can also tunnel connections over SSH instead of opening the DB port directly:
# Example SSH local port forward
ssh -L 5432:localhost:5432 user@<HOST_IP>
# Then connect to localhost:5432 on your client machine
psql -h localhost -p 5432 -U postgres -d postgres