supposed Docker is installed
A lightweight and official PostgreSQL container (Alpine) with automatic initialization, correct permissions handling, and ready-to-use configuration
In the docker-compose.yml, this crucial line is :
entrypoint: ["/bin/sh", "-c", "chown -R postgres:postgres /var/lib/postgresql/data && exec docker-entrypoint.sh postgres"]➡️ This line executes before startup :
chown -R postgres:postgres /var/lib/postgresql/data→ Give the correct permissions to the postgres user on the volume (even if it was created as root)
exec docker-entrypoint.sh postgres→ then run the real official initialization script.
✅ Résult :
No need for user : no manual chmod.
The image remains official and light (Alpine).
Works immediately on the first launch.
Run container :
docker-compose up -d
Check postgresql database docker image and the inserted data :
docker exec -i postgres_container psql -U postgres_user -d postgres_db < upsert_users.sql
docker exec -it postgres_container psql -U postgres_user -d postgres_db -c "SELECT * FROM users;"In terminal, result like this:
id | name | email
----+--------+---------------------
1 | Alice | alice@example.com
2 | Bob | bob@example.com
3 | Franck | franck@example.com
4 | Diana | diana@example.com
(4 rows)Example (Adminer, DBeaver, psql…) :
Host: localhostPort: 5432 (by default, not exposed, accessible by the Docker network)Database: postgres_dbUser: postgres_userPassword: password
If needed to start from scratch the database, run this command before the docker-compose up (this, will delete volume) :
docker-compose down -v