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
Create A Example Application Web PHP + Docker (Phpmyadmin , Mysql , Nginx)
First, create yourself a docker-compose.yml file and copy the code
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
docker-compose up -d
docker-compose ps
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
docker-compose exec -it <container-name>
Here our container name is "PostgresCount" so we will run the following command:
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"
docker-compose exec -it postgres_db bash
Okay, now that you're in root, the next thing is to login to postgres
# 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****
# psql -U hoadev -d hoadev_db
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,....)
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
If you find the article interesting, then contribute by sharing it with everyone
The article : Install PostgreSQL Using Docker Compose
Top comments (0)