- 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:
docker pull postgresAlternatively, you can pull preferred version with a specific tag:
docker pull postgres:15When postgres image is downloaded, you can check it in Images tab in Docker Desktop or by running docker images:
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest 75282fa229a1 6 weeks ago 453MBStart a Postgres instance
To start a new PostgreSQL container, run the following command:
docker run --name drizzle-postgres -e POSTGRES_PASSWORD=mypassword -d -p 5432:5432 postgres- The
--nameoption assigns the container the namedrizzle-postgres. - The
-e POSTGRES_PASSWORD=option sets thePOSTGRES_PASSWORDenvironment variable with the specified value. - The
-dflag runs the container in detached mode (in the background). - The
-poption maps port5432on the container to port5432on your host machine, allowing PostgreSQL to be accessed from your host system through this port. - The
postgresargument 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_USERenvironment variable with the specified value. Postgres uses the default user when this is empty. Most of the time, it ispostgresand you can check it in the container logs in Docker Desktop or by runningdocker logs <container_name>. - The
-e POSTGRES_DB=option sets thePOSTGRES_DBenvironment variable with the specified value. Defaults to thePOSTGRES_USERvalue when is empty.
To check if the container is running, check Containers tab in Docker Desktop or use the docker ps command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df957c58a6a3 postgres "docker-entrypoint.sā¦" 4 seconds ago Up 3 seconds 0.0.0.0:5432->5432/tcp drizzle-postgresConfigure database url
To connect to the PostgreSQL database, you need to provide the database URL. The URL format is:
postgres://<user>:<password>@<host>:<port>/<database>You should replace placeholders with your actual values. For example, for created container the url will be:
postgres://postgres:mypassword@localhost:5432/postgresNow you can connect to the database using the URL in your application.