How to set up Postgresql and Pgadmin with Docker
Tags: docker, postgres, beginners, tutorial
Published: 2021-06-23 11:11:00

In this post, we will learn how to install Postgresql and Pgadmin with Docker. If you are not familiar with it yet, please read [How to use Docker commands](https://dev.to/steadylearner/how-to-use-docker-commands-42da) first.

[You can see the Spanish version of this post here.](https://dev.to/steadylearner/como-instalar-postgresql-y-pgadmin-con-docker-4d5b)

[You can contact me with Telegram if you need to hire a full stack blockchain dev.](https://t.me/steadylearner)

[You can also join the Telegram group where I maintain and you can find other blockchain developers, recruiters, project owners, ask questions and network.](https://t.me/solanadevelopers)

If you use Linux relevant system, type `$docker` in your console and it would show how to install Docker.

![docker desktop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmrajw4gfyhgi4zxft9p.png)

If you use Mac or Windows, [it could be better to install the Docker desktop](https://www.docker.com/products/docker-desktop). Then, you can set it up to use minimal resource opitonally.

![set to use minimal resource](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpibfo11keudoicm5w8f.png)

## Table of contents

1. Install Postgresql and Pgamdin with Docker
2. Link them with Docker network
3. Set up Pgadmin and confirm connection
4. Conclusion

## 1. Install Postgresql and Pgadmin with Docker

Before we set up Postgresql with Docker, we will first make its data to persist in your machine with [volume](https://docs.docker.com/storage/volumes/).

```console
$docker volume create postgresqldata
```

Then, you can see it worked with `$docker volume ls` command.

```console
DRIVER    VOLUME NAME
local     postgresqldata
```

For volume to save your Postgresql data is ready, we will install it with Docker and link it to the volume we made with the command below.

```console
$docker run -d -v postgresqldata:/data/db -e POSTGRES_PASSWORD=postgres --name postgres -p 5432:5432 postgres
```

You can use your own password if you want after you complete this post.

When install is done, you will see the message similar to this in your console.

```console
Status: Downloaded newer image for postgres:latest
```

Then, use `$docker ps -a` to see it is saved ok.

```console
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                       NAMES
postgres   "docker-entrypoint.s…" 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres
```

Use `$docker exec -it postgres /bin/bash` to use the console of the Docker image and then `$psql -h localhost -U postgres` to use psql commands in your console.

You won't need this a lot if you install pgadmin later but you can change your password with `\password` later if you want after you complete this post.

We verified that Postgresql was installed ok with Docker. We will use another console to install Pgadmin to manage it easily with the command below.

```console
$docker run --name pgadmin -e "PGADMIN_DEFAULT_EMAIL=name@example.com" -e "PGADMIN_DEFAULT_PASSWORD=admin" -p 5050:80 -d dpage/pgadmin4 
```

Then, you will see the message similar to this.

```console
Status: Downloaded newer image for dpage/pgadmin4:latest
```

You can see it was installed ok with `$docker ps -a` again. Then, visit http://localhost:5050/login to see it is working ok and login to it with the username and password you used before.

![login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ou0a44435f8plxot290.png)

Then, you will be redirected to its admin page.

![admin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oepsqddbeoppbl5ryy3z.png)

You can set it while you [refer to this](https://docs.bitnami.com/installer/apps/canvaslms/administration/configure-pgadmin/) but it will not work yet.

## 2. Link them with Docker network and set up Pgadmin

To make Pgadmin installed with Docker work along with Postgesql, we should link them with the Docker network command.

We will first create pgnetwork for that purpose with the command below.

```console
$docker network create --driver bridge pgnetwork
```

Then, you can see it was created with `$docker network ls` command.

```console
NETWORK ID     NAME        DRIVER    SCOPE
               pgnetwork   bridge    local
```

You can link Pgadmin and Postgresql to it with these.

```console
$docker network connect pgnetwork pgadmin
$docker network connect pgnetwork postgres
```

You can verify they were connected with pgnetwork with `docker network inspect pgnetwork` command.

```console
[
    {
        "Name": "pgnetwork",
            {
                "Name": "pgadmin",
                "Name": "postgres",
            }
        },
    }
]
```

If you could make it here, everything is ready to make your Pgadmin with Postgresql with Docker.

## 3. Set up Pgadmin and confirm connection

Because we linked the Pgadmin and Postgresql installed with Docker in the same network, you can finally configure Pgadmin to connect with Postgresql.

Click new server button in [the admin browser page](http://localhost:5050/browser) first.

Then, use **postgres** for every necessary fields if you haven't changed anything yet.

![set up pgadmin general](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h19z3rk31dphmrv88ml0.png)

![set up pgadmin connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qapjb42yyfli8npun5te.png)

Then, click save button and you will see the connection is finally made.

![end connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x72d9x66m8mkaa05fbq.png)

Hope you could make it. [You can search more about how to use it at its website.](https://www.pgadmin.org/docs/pgadmin4/development/keyboard_shortcuts.html)

You can stop them after you test them not to use your resources with this.

```console
$docker stop pgadmin postgtres
```

If you want to use it with Python, these commands can be helpful.

```console
# Intel 
# $pip install psycopg2 or 

# M1  
# $brew install postgresql, $brew link openssl
# export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/icu4c/include"
# export PATH=/opt/homebrew/opt/postgresql@13/bin:$PATH
# export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
# export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
# export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"
# pip install psycopg2-binary
```
## 4. Conclusion

In this post, we learnt how to set up Postgresql and Pgamdin with Docker. You will be able to use it with a remote Postgresql instance also.

If you could make it work, you will not need to install Postgresql and pgadmin locally anymore.

[If you need to hire a developer, you can contact me.](https://t.me/steadylearner)

[I can write a full stack dapp.](https://dev.to/steadylearner/how-to-make-a-fullstack-dapp-with-react-hardhat-and-ether-js-with-examples-4fi2) 

Thanks.