Solution to Challenge: Running a Postgres Database

Learn the solution to the challenge you completed.

We'll cover the following

Solution

The following command will run the PostgreSQL:

docker run \
  -it --rm --name postgres \
  -p 3000:5432 \
  --mount "src=postgresdata,target=/var/lib/postgresql/data" \
  -e POSTGRES_PASSWORD=mysecret \
  postgres
  • -it runs an interactive session.

  • --rm removes the container after the container stops.

  • --name is used to name the container as postgres.

  • -p is used to define both the exposed port (left side of :) and the default port (right side of :) on which postgres is running.

  • --mount is used to define a persistent Docker volume named postgresdata.

  • -e allows the setting of required environmental variables. The default postgres user is created when not defining any user and the password is set using POSTGRES_PASSWORD.

πŸ“Œ Connect the terminal to see the above command in action.

Get hands-on with 1200+ tech skills courses.