macOS Docker(2) - postgres DB 설치 및 기본명령어
참고 : https://judo0179.tistory.com/48
참고 : https://hub.docker.com/_/postgres
1. 최신 postgres Docker 이미지 다운로드
- $ docker pull postgres
2. 포트매핑
- $ docker run -p 5432:5432 -e POSTGRES_PASSWORD=hjmin -e POSTGRES_USER=hjmin -e POSTGRES_DB=springdata --name postgres_boot -d postgres
- '-e' : 환경변수라는 의미
- '--name' : 컨테이너 이름
- '-d' : 데몬모드로 백그라운드로 띄우겠다는 의미
- postgres 설정 확인
> $ docker ps
3. 도커 컨테이너로 접근
- 명령어 : $ docker exec -i -t postgres_boot bash
- '-i' : interactive 모드
- '-t' : 타겟 컨테이너 명시
- 'bash' : 수행할 명령
- 명령어 수행 결과 : postgres_boot 컨테이너에 들어온 상태
- postgres DB 연결
> :/# su - postgres
> :~$ psql springdata
> 안됨
- 도커 컨테이너 삭제 : $ docker rm NAMES
> 예) $ docker rm postgres_boot
- postgres DB 연결 2차시도
> $ docker exec -i -t postgres_boot bash
> :/# su - postgres
> :~$ psql --username hjmin --dbname springdata
> 성공
4. Postgres 기본 명령어
4.1. =# \list : 데이터베이스 리스트 확인
4.2. =# \dt : 테이블 목록 확인
4.3. =# create database 데이터베이스이름 : 데이터베이스 생성
4.4. =# \c 데이터베이스이름 : 사용하는 데이터베이스 변경
5. Docker 명령어
5.1. $ docker ps -a : 내려가있는 컨테이너까지 모두 확인
5.2. $ docker [start | stop | rm] 컨테이너이름(NAMES) : 컨테이너 시작 | 중지 | 삭제