PostgreSQL | 데이터베이스(Database) | 데이터베이스 생성(CREATE DATABASE)

CREATE DATABASE 명령을 사용하여 PostgreSQL 데이터베이스를 새로 만드는 방법을 설명한다.

새 데이터베이스 생성하기

데이터베이스를 작성하려면 CREATE DATABASE 명령을 사용한다. 형식은 다음과 같다.

CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ TABLESPACE [=] tablespace_name ]
           [ ALLOW_CONNECTIONS [=] allowconn ]
           [ CONNECTION LIMIT [=] connlimit ]
           [ IS_TEMPLATE [=] istemplate ] ]

옵션이 여러가기가 준비되고, 기본이 되는 구문은 다음과 같다.

CREATE DATABASE name

데이터베이스 이름 (name)을 지정하여 새 데이터베이스를 생성한다. 명시적으로 지정하지 않으면 작성된 템플릿 데이터베이스 template1를 복사하여 데이터베이스가 만들어 진다.

데이터베이스를 만들려면 명령을 수행하는 역할이 수퍼 유저이거나 CREATEDB 권한을 가지고 있어야 한다.

그러면 실제로 해보도록 하자. psql에서 다음과 같이 실행한다.

C:\Users\kimkc>psql -U postgres
postgres 사용자의 암호:
psql (12.2)
도움말을 보려면 "help"를 입력하십시오.

postgres=# create database devkuma;
CREATE DATABASE
postgres=#

데이터베이스가 생성 되었다.

그러면 생성된 데이터베이스 조회해 보자.

postgres=# \l
                                      데이터베이스 목록
   이름    |  소유주  | 인코딩 |     Collate      |      Ctype       |      액세스 권한
-----------+----------+--------+------------------+------------------+-----------------------
 devkuma   | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |                        <<<< 신규 생성됨
 postgres  | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |
 sample    | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |
 template0 | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 | =c/postgres          +
           |          |        |                  |                  | postgres=CTc/postgres
 template1 | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 | =c/postgres          +
           |          |        |                  |                  | postgres=CTc/postgres
(5개 행)


postgres=#

문자 셋 및 데이터 정렬을 지정하여 데이터베이스를 만들기

데이터베이스를 만들 때 데이터베이스에서 사용하는 문자 셋 (ENCODING) 및 문자열 정렬 순서(LC_COLLATE)와 문자 분류(LC_CTYPE)는 명시적으로 지정하지 않은 경우에는 기본값이 사용되지만, 이 설정들은 한 번 데이터베이스를 만들면 변경할 수 없으므로 주의해야 한다.

만약 명시적으로 문자 셋을 지정하여 데이터베이스를 만들려면 다음 형식을 사용한다.

CREATE DATABASE name
  ENCODING encoding
  LC_COLLATE lc_collate
  LC_CTYPE lc_ctype

또한 로케일 설정 (LC_COLLATE와 LC_CTYPE)이 기본적으로 사용되는 템플릿 template1의 로케일 설정과 다른 경우에는 명시적으로 템플릿으로 template0를 사용해야 한다.

CREATE DATABASE name
  TEMPLATE template0
  ENCODING encoding
  LC_COLLATE lc_collate
  LC_CTYPE lc_ctype

그러면 실제로 해보도록 하자. 이번에는 문자 셋으로 UTF8 하고 문자열 정렬 순서과 문자 분류로 ‘Korean_Korea.949’을 설정하여 데이터베이스를 만든다.

postgres=# create database devkuma2
postgres-#   template template0
postgres-#   encoding UTF8
postgres-#   lc_collate 'Korean_Korea.949'
postgres-#   lc_ctype 'Korean_Korea.949';
CREATE DATABASE
postgres=#	

문자 셋, 문자열 정렬 순서, 그리고 문자 분류를 지정하여 새 데이터베이스를 생성하였다.

그러면, 생성 된 데이터베이스 나열 해 보자.

postgres=# \l
                                      데이터베이스 목록
   이름    |  소유주  | 인코딩 |     Collate      |      Ctype       |      액세스 권한
-----------+----------+--------+------------------+------------------+-----------------------
 devkuma   | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |
 devkuma2  | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |                        <<< 신규 생성
 postgres  | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |
 sample    | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 |
 template0 | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 | =c/postgres          +
           |          |        |                  |                  | postgres=CTc/postgres
 template1 | postgres | UTF8   | Korean_Korea.949 | Korean_Korea.949 | =c/postgres          +
           |          |        |                  |                  | postgres=CTc/postgres
(6개 행)


postgres=#

방금 만든 devkuma2 데이터베이스가 존재하고, 문자 셋와 문자열 정렬 순서, 문자 분류가 지정된 것으로 되어있는 것을 확인할 수 있다.

여기까지 CREATE DATABASE 명령을 사용하여 데이터베이스를 만드는 방법에 대해 설명하였다.




최종 수정 : 2020-11-12