본문 바로가기

PostgreSQL

DB 생성, 연결, 삭제

Database 생성

CREATE DATABASE test;

postgres=# CREATE DATABASE test;
CREATE DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

Database 연결

서버에서 psql --help 를 입력하면 일반적인 값들을 볼수 있음

연결 옵션들:
  -h, --host=HOSTNAME      데이터베이스 서버 호스트 또는 소켓 디렉터리
                           (기본값: "로컬 소켓")
  -p, --port=PORT          데이터베이스 서버 포트(기본 값: "5432")
  -U, --username=USERNAME  데이터베이스 사용자 이름(기본 값: "root")
  -w, --no-password        암호 프롬프트 표시 안 함
  -W, --password           암호 입력 프롬프트 보임(자동으로 처리함)
[root@localhost ~]# sudo -u postgres psql
could not change directory to "/root": 허가 거부
psql (10.10)
Type "help" for help.

postgres=# \c test postgres
You are now connected to database "test" as user "postgres".
test=# 
DATABASE 삭제

DROP

위험 하기 때문에 항상 체크하고 사용할 것

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(4 rows)

postgres=# DROP DATABASE test; 
DROP DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

'PostgreSQL' 카테고리의 다른 글

DB 리스트 보는 방법  (0) 2020.03.20
Postgres 시작하기  (0) 2020.03.20