PostgreSQL

[PostgreSQL] PostgreSQL 기초 Database 및 User 생성

dbdb딥 2023. 9. 4. 17:14
반응형
[PostgreSQL] PostgreSQL 기초 Database 및 User 생성

 

User Create 작성방법
CREATE USER name [ [ WITH ] option [ ... ] ]

위 옵션에 대한 간략한 설명

SUPERUSER | NOSUPERUSER ; Superuser 여부. 기본값은 NOSUPERUSER이다.
CREATEDB | NOCREATEDB ; DB생성 권한 부여 여부. 기본값은 권한 없음 이다.
CREATEUSER | NOCREATEUSER ; User생성 권한 부여 여부. 기본값은 권한 없음 이다.
PASSWORD 'password' ; Password 설정

 

user 생성
## 생성 시

postgres=# create user test password 'test' createdb login;
CREATE ROLE

또는 

postgres=# create user test password 'test' createdb login;
CREATE ROLE

postgres=# \du
                                롤 목록
 롤 이름  |                      속성                      | 소속 그룹:
----------+------------------------------------------------+------------
 postgres | 슈퍼유저, 롤 만들기, DB 만들기, 복제, RLS 통과 | {}
 test     | DB 만들기                                      | {}

postgres=# SELECT * FROM PG_USER;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |
 test     |    16384 | t           | f        | f       | f            | ******** |          |
(3개 행)

postgres=#

 

## 삭제 시
postgres=# drop user test;
DROP ROLE

또는 

postgres=# drop role test;
DROP ROLE

postgres=# \du
                                롤 목록
 롤 이름  |                      속성                      | 소속 그룹:
----------+------------------------------------------------+------------
 postgres | 슈퍼유저, 롤 만들기, DB 만들기, 복제, RLS 통과 | {}

postgres=# select * from pg_user;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |
(1개 행)

postgres=#

 

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 ] ]

위 Database 생성 문법에 대한 옵션 간략한 설명

  • OWENR : DB owne
  • TEMPLATE : DB Template에 의해 생성될 Template
  • ENCODING : Data Encoding 방법. 값을 지정할 때 LC_CTYPE, LC_COLLATE value와 연계되기 때문에 주의
  • LC_COLLATE : String Data를 기준으로 정렬할 때 정렬 기준. 예를 들면 ko_KR.UTF-8은 기본적으로 한글 기준으로 정렬하되, 한글 외의 문자는 UTF-8에 의해 정렬하라는 의미다. 본 시스템 설치 시 ko_KR.UTF-8이 기본값으로 설정 (template1의 기본값)
  • LC_CTYPE : 대, 소문자, 숫자 등과 같은 문자 분류를 위한 설정.
  • TABLESPACE : Table Space를 임의로 설정할 때 사용.
  • ALLOW_CONNECTIONS : 외부에서 접속 가능 여부 설정
  • CONNECTION LIMIT : DB 접속 제한 설정
  • IS_TEMPLATE : DB Template 인지 여부 설정

 

Database 생성
## 기본생성
postgres=# create database dbdbdeep;
DROP DATABASE


## 템플릿 데이터베이스 이용하여 생성
postgres=# create database dbdbdeep template template1;
CREATE DATABASE


## 데이터베이스 Owner 포함
postgres=> create database dbdbdeep owner test;
CREATE DATABASE
postgres=>
postgres=> \l
                                 데이터베이스 목록
   이름    |  소유주  | 인코딩 |   Collate   |    Ctype    |      액세스 권한
-----------+----------+--------+-------------+-------------+-----------------------
 dbdbdeep  | test     | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 |
 postgres  | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 |
 template0 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +
           |          |        |             |             | postgres=CTc/postgres
(4개 행)


## 접속
dbdbdeep=# \c postgres postgres
접속정보: 데이터베이스="postgres", 사용자="postgres".
postgres=#
postgres=#
postgres=#
postgres=#
postgres=#
postgres=# \c dbdbdeep test
접속정보: 데이터베이스="dbdbdeep", 사용자="test".
dbdbdeep=> select current_user;
 current_user
--------------
 test
(1개 행)

dbdbdeep=> select current_database();
 current_database
------------------
 dbdbdeep
(1개 행)

dbdbdeep=>

 

 

반응형
맨 위로