How to start PostgreSQL server in Docker

Running PostgreSQL in a Docker container offers flexibility and ease of use, especially for development and testing environments. It eliminates the need to install PostgreSQL directly on your machine, ensuring a clean and isolated environment. With Docker, you can easily manage different PostgreSQL versions, share configurations, and replicate setups for consistent development workflows.

Starting PostgreSQL on Docker

docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=change-me -d postgres

Check logs

docker logs postgres

Note. Password is "change-me"

Connect to DB from within the Docker container (no need to have the PostgreSQL client installed)

docker exec -it postgres psql -U postgres

Connect to DB from local (if the PostgreSQL client is installed)

psql -h 127.0.0.1 -p 5432 -U postgres