Starting a Postgres Docker container

To begin, you will start a Postgres Docker container in your terminal:

$ docker run --name postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=users -p 5432:5432 -d postgres

This will start a Postgres container with some basic setup:

  • --name sets the name of the container
  • -e allows us to set environment variables:
    • POSTGRES_PASSWORD: The password used to access the database
    • POSTGRES_DB: The name of the database
      • -p allows us to expose port 5432 on the container to port 5432 on our local machine
      • -d allows us to start the container in daemon mode (runs in the background)
If you are creating a database for a production environment then it is important to set a more secure password and keep it safe!

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.