Install PostgreSQL Using Docker Compose
Tags: webdev, docker, postgres, postgressql
Published: 2023-10-17 15:23:36

Now I share with everyone how to install PostgreSQL using Docker Compose . When it comes to Docker, everyone has probably used it before. In previous articles I also shared about Docker , you can review it here.

[Create A Application Laravel 8 + Docker](https://hoanguyenit.com/create-a-application-laravel-8-docker.html)

[Create A Example Application Web PHP + Docker (Phpmyadmin , Mysql , Nginx)](https://hoanguyenit.com/create-a-example-application-web-php-docker-phpmyadmin-mysql-nginx.html)

First, create yourself a **docker-compose.yml** file and copy the code

```javascript
version: '3.8'
services:
    postgres_db:
        image: postgres:13.5
        container_name: PostgresCount
        restart: always
        environment:
            - POSTGRES_USER=hoadev
            - POSTGRES_PASSWORD=hoadev123
            - POSTGRES_DB=hoadev_db
        volumes:
            - postgres_db:/var/lib/postgresql/data
        ports:
            - '5432:5432'
volumes:
    postgres_db:
        driver: local
```

- **version** : specifies the version of the Docker Compose file. In my case, it is "3.8".

- **service** : defines the services that need to be executed using Docker Compose

- **postgres_db** : is the name of the Postgres service.

- **image** : defines the image to use, i.e. "postgres:latest ".

- **container_name** : represents the container name, i.e. PostgresCont . it is the name of the container

- **environment** : set environment variables in Postgresql. For example: POSTGRES_USER , POSTGRES_PASSWORD , POSTGRES_DB ,...

- **port** : set the communication port for it " 5432:5432 "

- **volumes** : set up a volume to persist Postgres data

Okay, now let's open the command and run the command to run it

```javascript
docker-compose up -d
docker-compose ps
```

![Install PostgreSQL Using Docker Compose - hoanguyenit.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yead321ky681e6lfqcha.jpg)

Above you have run successfully, you will see the name of the container is "**PostgresCount**"

Now our job is to run the bash command, to access the container 

```javascript
docker-compose exec -it <container-name>

```

Here our container name is "**PostgresCount**" so we will run the following command:

```javascript
docker-compose exec -it PostgresCount bash

```

If you run the above command and you encounter this message: 

**_service "PostgresCount" is not running_**

Don't worry, we have a way to run the above command, by directly running the name of the service "**postgres_db**"

```javascript
docker-compose exec -it postgres_db bash

```

![Install PostgreSQL Using Docker Compose - hoanguyenit.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smgy1o0g2gy5haubps7l.jpg)

Okay, now that you're in root, the next thing is to  **login** to postgres

```javascript
# psql -U <user> -d <database>
```

**user** : user name that we installed in the environment, of the **docker-compose.yml** file

**database** : the database name is also named in the environment****

```javascript
# psql -U hoadev -d hoadev_db

```

![Install PostgreSQL Using Docker Compose - hoanguyenit.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g585e019v3xy7tkgilhp.jpg)

You can run the command **\l** to display all databases

Okay, you have successfully entered this point, the rest you just need to write a few query statements in SQL like (create, read, delete, select,....)

![Install PostgreSQL Using Docker Compose - hoanguyenit.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndiuxgb2yexxoer3i8t1.jpg)

Okay, let's learn more about query statements in postgresql to test, see you in the next sharing post. You can see more here: 

[How To Create Data Queries in PostgreSQL By Using the Select Command](https://www.digitalocean.com/community/tutorials/how-to-create-data-queries-in-postgresql-by-using-the-select-command)

If you find the article interesting, then contribute by sharing it with everyone

The article : [Install PostgreSQL Using Docker Compose](https://hoanguyenit.com/install-postgresql-using-docker-compose.html)