Monday, 10 April 2023

Using PostgreSQL DB by Docker

1. Create docker-compose.yml

version: '3.8'
services:
db:
image: postgres:14.1-alpine
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
driver: local 

2. Using container postgres

# create container
docker compose up
 
# find id of container
docker ps

# access container
docker exec -it 0d5ed17f72bb /bin/sh

# access postgre
psql --username postgres

# example create myUser & myDB
CREATE USER myuser WITH PASSWORD 'myPassword';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser

# show all DB, all users
\l
\du

 Thank you

No comments:

Post a Comment

Golang Advanced Interview Q&A