도커 이미지 설치하기

 

postgres - Official Image | Docker Hub

Note: the description for this image is longer than the Hub length limit of 25000, so has been trimmed. The full description can be found at https://github.com/docker-library/docs/tree/master/postgres/README.md. See also docker/hub-feedback#238 and docker/

hub.docker.com

 

위 링크에서 Tags 탭으로 들어가면 버전을 확인할 수 있는데 원하는 버전을 선택해서 docker pull postgres:latest  복사해주면된다.

 

복사한 명령어를 터미널에서 입력해 설치해준다.

$ docker pull postgres:latest



latest: Pulling from library/postgres
9e3ea8720c6d: Pulling fs layer
7782b3e1be4b: Pulling fs layer
247ec4ff783a: Pulling fs layer
f7ead6900700: Pulling fs layer
e7afdbe9a191: Pulling fs layer
3ef71fe7cece: Pulling fs layer
1459ebb56be5: Pulling fs layer

...
...
...

5f7e3ef14a3b: Pull complete
Digest: sha256:78a275d4c891f7b3a33d3f1a78eda9f1d744954d9e20122bfdc97cdda25cddaf
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

 

이미지 설치 후 이미지를 확인할 수 있다.

$ docker images


REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
postgres     latest    bf700010ce28   7 days ago   379MB

 

 

 

 

 

 

도커 컨테이너 생성

설치한 이미지로 도커 컨테이너를 생성한다. <password> 자리에 사용할 비밀번호를 입력하면 된다.

$ docker run -d -p 5432:5432 -e POSTGRES_PASSWORD="<password>" --name PostgreSQL01 postgres

0f75921476aaee3909befcab2a514dd50ad4d22db4dad0af2b03c82e2bccff08

 

아래 명령어로 생성된 컨테이너를 확인 할 수 있다.

$ docker ps -a


CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                    NAMES
0f75921476aa   postgres   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:5432->5432/tcp   PostgreSQL01

 

컨테이너가 실행 중일 때 컨테이너로 진입 시 아래 명령어를 입력한다.

$ sudo docker exec --user="root" -it PostgreSQL01 "bash"

root@0f75921476aa:/#

 

 

컨테이너에 진입 후 PostgreSQL를 실행하려면 아래명령어를 입력한다.

psql -U postgres


psql (15.2 (Debian 15.2-1.pgdg110+1))
Type "help" for help.

postgres=#

 

 

 

PostgreSQL 설정

계정을 생성해준다. 아이디 whitewise  비밀번호 1111로 생성한다.

CREATE USER whitewise WITH PASSWORD '1111';

CREATE ROLE

 

 

계정에 Role 추가하기 whitewise 유저에게 슈퍼유저를 부여한다.

ALTER ROLE whitewise CREATEDB Superuser;

ALTER ROLE

 

 

추가된 Role 조회하기

\du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 whitewise | Superuser, Create DB

 

 

DB 생성 -  springsecurity라는 DB를 생성한다.

create database springsecurity;

CREATE DATABASE

 

 

DB에 접근하려면 아래 명령을 입력한다.

\c springsecurity

You are now connected to database "springsecurity" as user "postgres".
springsecurity=#

 

 

스키마생성은 아래 명령어들을 참고한다.

-- 스키마 생성 + authorization user 를 해서 테이블 생성이 가능하도록 함.
create schema if not exists test authorization postgres;

-- 확인
select * from pg_catalog.pg_namespace pn 
order by nspname;

-- 생성한 schema 를 기준으로만 쿼리가 동작하도록 세팅
set search_path to test;

-- 제대로 스키마가 적용되었는지 확인
show search_path;
복사했습니다!