[PostgreSQL] - Simple and Quick Start a PostgreSQL Server with Docker

Posted by William Basics on Wednesday, January 5, 2022

Introduction

There are already some guides to introduce ways to start a PostgreSQL Server with Docker. But none of them helps me to work it out. That’s why I wrote my version to note the problems I encountered and their solutions.

The environment

My working computer is Windows 10, and the version of the docker desktop is 4.3.2.

The Steps

1. Get the PostgreSQL docker image

You can find the docker image at https://hub.docker.com/_/postgres. Run the command to pull the image.

docker pull postgres

2. Start the PostgreSQL server container

The official guide provides this command to us.

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
# can't work

It would be working, but not. The container runs, but my database client can’t connect to the server. The reason is that it doesn’t set the port mapping.

So the working command shall be this

docker run --name dev-postgre -e POSTGRES_PASSWORD=123 -d -p 5432:5432 postgres

Run docker ps to check the container is running or not. docker

The container is running as expected.

3. Connect to the PostgreSQL server

To be simplified, I connect to the database in VSCode by using the MySQL extension. docker

Type in the password and click the connect button. The server is connected as expected. docker

Now, we can develop our application with PostgreSQL.

Conclusion

This guide is really simple. Next time, I will introduce more complex scenarios.

(end.)