Hello, I'm Maneshwar. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.
---When running PostgreSQL inside a Docker containe
r, one common problem is data loss if the container is restarted or deleted.
This post shows how to persist PostgreSQL data using Docker volume mounts so your database survives container restarts.
Why Persistence Matters
By default, Docker containers are ephemeral. When they stop or are removed, any data inside them is lost — unless you've mapped it to persistent storage.
PostgreSQL stores all its data in /var/lib/postgresql/data. To persist that, you need to mount a volume from your host machine or a Docker-managed volume.
PostgreSQL with Named Volume
Here's a quick docker-compose.yml setup using a named volume:
version: '3.8'
services:
db:
image: postgres:16
container_name: pg-container
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
pgdata:
Restart & Data Survives
docker-compose up -d
# Create a table or insert some data
docker-compose down
docker-compose up -d
# Data is still there
The pgdata volume is managed by Docker and won't be deleted unless you explicitly remove it.
PostgreSQL with Host Bind Mount
If you want the data stored in a specific host directory (e.g. ./pgdata), modify the compose like this:
services:
db:
image: postgres:16
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
POSTGRES_DB: mydb
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
Make sure ./pgdata exists and is writable by Docker. This method is great if you want to inspect or back up the files directly from the host.
Check Volume Status
docker volume ls
docker volume inspect <volume_name>
To clean up:
docker volume rm pgdata
Testing Persistence
Run a test inside the container:
docker exec -it pg-container psql -U admin -d mydb
# Inside psql:
CREATE TABLE test (id SERIAL PRIMARY KEY, name TEXT);
INSERT INTO test (name) VALUES ('docker-pg');
\q
Now restart the container:
docker-compose down
docker-compose up -d
Recheck the table:
docker exec -it pg-container psql -U admin -d mydb -c "SELECT * FROM test;"
Your data is still there.
Common Gotchas
- Make sure the volume directory (host or named) has the correct permissions.
- Avoid mounting empty host dirs over
/var/lib/postgresql/dataif you already ran the container before. - Don't forget to back up your volume if you're using named volumes — they're not visible in the filesystem.
Conclusion
Using Docker volumes, you can safely run PostgreSQL in containers without worrying about data loss on restart. Use named volumes for simplicity, or bind mounts for full control.
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*
Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.
⭐ Star it on GitHub:
HexmosTech
/
git-lrc
Free, Unlimited AI Code Reviews That Run on Commit
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.
See It In Action
See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements
git-lrc-intro-60s.mp4
Why
- 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
- 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
- 🔁 Build a habit, ship better code. Regular review → fewer bugs → more robust code → better results in your team.
- 🔗 Why git? Git is universal. Every editor, every IDE, every AI…
Top comments (0)