PostgreSQL Alpine 16 Docker Installation Guide
Quick Start
docker run --name postgres16 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:16.3-alpine3.20
Detailed Steps
- Pull the image:
docker pull postgres:16.3-alpine3.20
-
Run the container:
# When running the PostgreSQL container for the first time using Docker, # you are required to set the environment variable POSTGRES_PASSWORD to your desired password for the PostgreSQL superuser. docker run --name postgres16 -e POSTGRES_PASSWORD=mysecretpassword -d postgres:16.3-alpine3.20
docker run
: Command to create and start a new Docker container.--name postgres16
: Assigns a name (postgres16
) to the container for easy reference.-e POSTGRES_PASSWORD=mysecretpassword
: Sets an environment variablePOSTGRES_PASSWORD
inside the container with the valuemysecretpassword
. This is required to set the password for the PostgreSQL superuser.-
-d
: Runs the container in detached mode (in the background).Run the existing container
-
#list container docker ps -a
-
docker start <container_id> #run the container docker start -a <container_id> #see the forground activity -a [attach]
- Connect to the database:
docker exec -it postgres16 psql -U postgres
Environment Variables
POSTGRES_PASSWORD
: Set the superuser password (required)POSTGRES_USER
: Create a new superuser (default: postgres)POSTGRES_DB
: Specify the name of the default database (default: POSTGRES_USER value)
Persistence
Mount a volume for data persistence:
docker run --name postgres16 -e POSTGRES_PASSWORD=mysecretpassword -v /my/data:/var/lib/postgresql/data -d postgres:16-alpine
Custom Configuration
Mount a custom postgresql.conf
:
docker run --name postgres16 -e POSTGRES_PASSWORD=mysecretpassword -v /my/custom/postgresql.conf:/etc/postgresql/postgresql.conf -d postgres:16-alpine -c 'config_file=/etc/postgresql/postgresql.conf'
Health Check
docker run --name postgres16 -e POSTGRES_PASSWORD=mysecretpassword -d --health-cmd="pg_isready -U postgres" --health-interval=10s --health-timeout=5s --health-retries=5 postgres:16-alpine