Docker PostgreSQL DataBase


Делаю:
2024.11.11


$ mkdir -p ~/projects/dev/db/postgres/
$ cd ~/projects/dev/db/postgres/


$ sudo apt install -y postgresql-client-common postgresql-client


$ sudo vi /etc/hosts


127.0.0.1 postgreshost


$ vi .env


DATABASE_HOST=postgreshost
DATABASE_NAME=postgresdb
DATABASE_PORT=5432
DATABASE_USER=admin1
DATABASE_PASSWORD=pA55w0rd123


$ vi docker-compose.yml


version: '3'
services:
  postgres:
    container_name: postgres
    image: postgres:15.2-alpine3.17
    restart: always
    hostname: ${DATABASE_HOST}
    ports:
      - ${DATABASE_PORT}:5432
    environment:
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
    volumes:
      - ./PGDATA:/var/lib/postgresql/data


$ docker-compose up


Example:
https://github.com/webmakaka/Uber-Eats-Clone


Import data into postgresql from sql dump file

$ cd data/

// Connect
$ PGPASSWORD=pA55w0rd123 psql -U admin1 -h postgreshost -p 5432 -d postgresdb

// Import data from sql
// $ PGPASSWORD=pA55w0rd123 psql -U admin1 -h postgreshost -p 5432 -d postgresdb < go_movies.sql


$ PGPASSWORD=pA55w0rd123 psql --host=postgreshost --username=admin1 --port=5432 --dbname=go_movies -c 'select id, title, description, year, release_date, rating, runtime, mpaa_rating, created_at, updated_at from movies where id = 1'


$ PGPASSWORD=pA55w0rd123 psql --host=postgreshost --username=admin1 --port=5432 --dbname=go_movies -c "INSERT INTO movies_genres (movie_id, genre_id, created_at, updated_at) VALUES (1, 1, '2021-05-19', '2021-05-19');"


Run pgadmin in docker container:

$ docker run -e PGADMIN_DEFAULT_EMAIL='[email protected]' -e PGADMIN_DEFAULT_PASSWORD='password1234' -p 5555:80 --name pgadmin dpage/pgadmin4


http://localhost:5555/

login: [email protected]
pass: password1234


To connect, use host ip address


Docker-compose config with postgres and pgadmin


From video course


services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: amigoscode
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - '5432:5432'
    networks:
      - postgres
    restart: unless-stopped

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - '5050:80'
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin: