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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay