To run PostgreSQL on Docker

Run PostgreSQL on docker:

Pull the Image from Docker Hub

docker pull postgres

Verify the downloaded image

λ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            latest              b97bae343e06        3 weeks ago         313MB
alpine              latest              a24bb4013296        4 weeks ago         5.57MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

Volume Container

Create a Volume Container for persisting data generated by and used by Docker containers,here the PostgeSQL container. Otherwise the data would be lost in case if you want to remove the PostgreSQL container. the -v parameter should match the PostgreSQL default data path.

λ docker create -v /var/lib/postgresql/data --name postgresql-data alpine

Run PostgreSQL with volumne container

λ docker run --name postgres1 -e POSTGRES_PASSWORD=password123 -d --volumes-from postgresql-data postgres

Verify db status

λ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9426aa235613        postgres            "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds       5432/tcp            postgres1

Connect to PostGreSQL

To Connect to PostgreSQL, we might need to use the network IP address. First find the network details of docker:

λ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9a74c109118a        bridge              bridge              local
38c6238bcf19        host                host                local
5da6ef33f9d0        none                null                local

Here, ‘bridge’ is our network. Lets find the IP address for the postgres:

λ docker network inspect bridge
        "Name": "bridge",
        "Id": "9a74c109118a93cdbcdb43da734d0275ad1ea57fa1580a231067d54d074a352d",
        "Created": "2020-07-02T23:51:23.8227395Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                    "Subnet": "",
                    "Gateway": ""
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        "ConfigOnly": false,
        "Containers": {
            "8a598f453cab6034ee76415d8c1456214e4111d2332d72e08b3ad8959c99fdb3": {
                "Name": "elegant_babbage",
                "EndpointID": "fa8ac23b634a95738ff8e11e6ecb3b72c4d39ac08859b2898f7664840b7a00e9",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "",
                "IPv6Address": ""
            "eeb90789f0f66d1c44560efc0287e49f04cfe66a05f32d48f4c50c9096c1f39f": {
                "Name": "postgres1",
                "EndpointID": "d6f68665bccc4f59767c451ef77cf913a3dbac2e5143b93eea2b43b77730ff25",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "",
                "IPv6Address": ""
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        "Labels": {}

From above output, we can deduct that the postgres1 IP is Lets connect using psql:

λ docker run -it --rm --network bridge postgres psql -h -U postgres
Password for user postgres:
psql (12.3 (Debian 12.3-1.pgdg100+1))
Type "help" for help.



We can also use PgAdmin to connect to the Database

Pull the PgAdmin image

λ docker pull dpage/pgadmin4
Using default tag: latest
latest: Pulling from dpage/pgadmin4
Digest: sha256:e653453cbeeba098bae4de9c7c3388278d4b695814f2f6d25ceed2a02ce7887c
Status: Downloaded newer image for dpage/pgadmin4:latest

Run PgAdmin docker

λ docker run -p 80:80 -e "PGADMIN_DEFAULT_EMAIL=user@domain.com" -e "PGADMIN_DEFAULT_PASSWORD=password123" -d dpage/pgadmin4

Now we will have PostgreSQL and PgAdmin running:

λ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
eeb90789f0f6        postgres            "docker-entrypoint.s…"   14 minutes ago      Up 14 minutes>5432/tcp        postgres1
8a598f453cab        dpage/pgadmin4      "/entrypoint.sh"         18 minutes ago      Up 18 minutes>80/tcp, 443/tcp   elegant_babbage

Connect with PgAdmin

Open the PgAdmin in browser @ http://localhost

Add a new server:

Enter Connection information and Save:

And its connected: