DEV Community

Shaikh Al Amin
Shaikh Al Amin

Posted on • Edited 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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay