- Install latest Docker Desktop. Follow the instructions for your operating system.
Pull the PostgreSQL image
Pull the latest PostgreSQL image from Docker Hub. In your terminal, run docker pull postgres
to pull the latest Postgres version from Docker Hub:
Alternatively, you can pull preferred version with a specific tag:
When postgres image is downloaded, you can check it in Images
tab in Docker Desktop or by running docker images
:
Start a Postgres instance
To start a new PostgreSQL container, run the following command:
- The
--name
option assigns the container the namedrizzle-postgres
. - The
-e POSTGRES_PASSWORD=
option sets thePOSTGRES_PASSWORD
environment variable with the specified value. - The
-d
flag runs the container in detached mode (in the background). - The
-p
option maps port5432
on the container to port5432
on your host machine, allowing PostgreSQL to be accessed from your host system through this port. - The
postgres
argument specifies the image to use for the container. You can also specify other versions likepostgres:15
.
You can also specify other parameters like:
- The
-e POSTGRES_USER=
option sets thePOSTGRES_USER
environment variable with the specified value. Postgres uses the default user when this is empty. Most of the time, it ispostgres
and you can check it in the container logs in Docker Desktop or by runningdocker logs <container_name>
. - The
-e POSTGRES_DB=
option sets thePOSTGRES_DB
environment variable with the specified value. Defaults to thePOSTGRES_USER
value when is empty.
To check if the container is running, check Containers
tab in Docker Desktop or use the docker ps
command:
Configure database url
To connect to the PostgreSQL database, you need to provide the database URL. The URL format is:
You should replace placeholders with your actual values. For example, for created container the url will be:
Now you can connect to the database using the URL in your application.