DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on • Updated on

How to run postgres database using docker and docker-compose

Using docker-compose:

Create a directory postgresql_data.

mkdir postgresql_data
Enter fullscreen mode Exit fullscreen mode

Create docker volume for postgresql_data:

docker volume create postgresql_data
Enter fullscreen mode Exit fullscreen mode

*Now create a docker-compose.yml file and paste the following code: *

version: '3.8'
services:
  pg_database:
    image: postgres:14.9-alpine
    restart: always
    volumes:
      - ./postgresql_data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    ports:
      - '5455:5432'
volumes:
  postgresql_data:
Enter fullscreen mode Exit fullscreen mode

*Now run: *

docker-compose up --build -d
Enter fullscreen mode Exit fullscreen mode

Install only postgres client for terminal:

sudo apt install postgresql-client
Enter fullscreen mode Exit fullscreen mode

Connect from the terminal to your database:

psql -U postgres -p 5455 -h localhost;

Enter fullscreen mode Exit fullscreen mode

Now create your own database to start working :

create database ecommerce_crud;
Enter fullscreen mode Exit fullscreen mode

*Insert dump sql into database : *

psql -U postgres -p 5455 -h localhost -d nodalnda < nodal_dump_local2024_04_29_20_00.sql
Enter fullscreen mode Exit fullscreen mode

Postgres Basic command:


 \l ; is same as show databases in mysql;
 \d+ products; is same as show create table products ;
 \c morning_fresh_bakery; same as  use country_db in mysql;
 \dt; show tables in mysql;
 \d users describe table;

DROP DATABASE nodalnda WITH (FORCE);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)