Notes taken by Horeb S
Links
Table of contents
In this part of the course, we will learn how to run PostGres in Docker and then, ingest a Database in it. The code to run PostGres in Docker is as following :
docker run -it \\
-e POSTGRES_USER="root" \\
-e POSTGRES_PASSWORD="root" \\
-e POSTGRES_DB="ny_taxi" \\
-v ny_taxi_postgres_data:/var/lib/postgresql/data \\
-p 5432:5432 \\
postgres:13
Let's break down this command to understand what each flag does:
-e
flags set environment variables for the container (username, password, database name)
-v
flag creates a volume to persist data even if container is stopped. The volume is mapped to /var/lib/postgresql/data
inside the container, which is where PostgreSQL stores its data files. This ensures our data, originally in the host machine's ny_taxi_postgres_data
volume, survives container restarts or removals.
<aside> ⚠️
This works correctly on Linux. However, on Windows, instead of ny_taxi_postgres_data
, you need the absolute path. For compatibility across both Linux and Windows, simply use -v $(pwd)/ny_taxi_postgres_data:var/lib/postgresql/data \\\\
where$(pwd)
prints your current directory path.
</aside>
-p
flag maps port 5432 from container to host machine.
POSTGRES_USER="root"
is the username we want to set for accessing our PostgreSQL database. In this case, we're using "root" as a simple example, though in production we'd want to use a more secure username. POSTGRES_PASSWORD="root"
sets the password for accessing the database. Again, in a production environment, we would use a much stronger password. POSTGRES_DB="ny_taxi"
specifies the name of the database that will be created when the container starts up.
If everything is running smoothly then you will see a bunch of files generated in the ny_taxi_postgres_data folder.