Running PostgreSQL using Docker
Updated
Running PostgreSQL via Docker is one of the things you can do to quickly try postgresql without installing or configuring it locally. It's one of the benefits of using Docker. In this quick tutorial, you will get to know how you can run postgresql inside a docker container terminal using psql (a terminal interface to interact with postgresql databases).
Pulling Official PostgreSQL Image
Pull the official image of postgresql from Docker Hub using the docker pull command.
docker pull postgresYou can specify the version of postgresql to pull.
docker pull postgres:latest
docker pull postgres:16This will create a postgres image. You can verify that using the docker images command.
docker images
# output
REPOSITORY TAG IMAGE ID CREATED SIZE
postgres latest cff6b68a194a 5 weeks ago 432MBRunning the Image
Running the image means executing the image inside an isolated environment known as a container. It can be done using the docker run command.
docker run --name pgcontainer -e POSTGRES_PASSWORD=specify_your_password -d postgresLet's understand each flag/argument:
--name:- the name of the container to be created. (example:pgcontainer)-e:- environment variables to be fed into the container (here:POSTGRES_PASSWORD=specify_your_password).-d:- runs the container in a detached mode (in background) without blocking the terminal.- the last argument
postgresis the name of the image.
Run docker run --help for more information.
You can verify the container is running by using the docker ps command which will list all the running containers. A status of Up means the container is up and running.
docker ps
# Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ebeab2ca77e9 postgres "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp pgcontainerExecuting Commands Inside Container
You can execute commands inside the container by accessing it's terminal shell using the docker exec command. The -it flag specifies the mode (interactive) in which you want to access the shell. It is meant to keep the input stream alive even if the container is in detached mode. The below command will spin up a psql instance which you can use to create and interact with databases.
docker exec -it pgcontainer psql -U postgresWhen you execute the above command, you can interact with the psql interface as shown below:
psql (16.3 (Debian 16.3-1.pgdg120+1))
Type "help" for help.
postgres=# |Conclusion
This is just one of the ways you can use postgres' image independently using docker. For more advanced and integrated usage of postgres along with multiple services, consider using docker compose.