PostgreSQL Syntax
PostgreSQL 문법
PSQL Command
- PSQL의 CUI 환경에서 사용하는 명령어이다.
\?
\?
- PSQL Command 관련 도움말을 출력한다.
\h
\h
- SQL Commnad 관련 도움말을 출력한다.
\c [DB_name] [Connection_User]
\c [DB_Name] [Connection_User]
- Query를 실행할 DB Instance에 입장한다.
\list or \l
\list
-- 또는
\l
- 전체 DB Instance 리스트를 출력한다.
\d[t | s | f | v | u] [+]
\dt
-- 현재 접속중인 DB Instance의 Table 목록을 출력한다.
\ds
-- Sequence 목록을 출력한다.
\df
-- Function 목록을 출력한다.
\dv
-- View 목록을 출력한다.
\du
-- User 목록을 출력한다.
\dn
-- Schema 목록을 출력한다.
\di
-- Index 목록을 출력한다.
- 현재 접속중인 DB Instance 혹은, DB 시스템 전체에 관한 정보들을 출력한다.
- 뒤에 '+'를 추가할 경우, 상세 정보를 포함하여 출력시킬 수 있다.
\d[+] [Table_Name]
\d[+] [Table_Name]
- 현재 DB Instance 내에 정의되어 있는 Table_Name 테이블의 구조를 출력한다.
- 테이블에 정의되어 있는 Column, Type, Collation, Nullable, Default 가 출력된다.
- Table_Name 앞에 +를 추가할 경우, 상세 정보를 포함하여 출력시킬 수 있다.
\g
\g
- 바로 이전에 실행했던 명령어를 다시 실행한다.
- 위, 아래 방향키(↑,↓)로 Command History를 확인해가며 다시 실행하는 편이 더 확실하고, 안전하다.
\s
\s
- 이전에 실행했던 명령어 전체를 조회한다.
\x
\x
- Column들을 한 줄로 조회하기 어려울 경우, Column들을 세로로 배치하여 출력하는 기능을 On/Off한다.
\a
\a
- Column Align 기능을 On/Off한다.
- Default는 Align On 이다. (즉, 켜져 있다.)
\H
\H
- Column명과 결과값을 HTML Table 형식으로 출력하는 기능을 On/Off한다.
\i [File_Name]
\i [File_Name]
- 외부 SQL 파일로 Query를 실행한다.
\q
\q
- PostgreSQL을 종료한다.
PSQL Data Types
| Name | Aliases | Description |
| bigint | int8 | signed eight-byte integer |
| bigserial | serial8 | autoincrementing eight-byte integer |
| bit [ (n) ] | fixed-length bit string | |
| bit varying [ (n) ] | varbit [ (n) ] | variable-length bit string |
| boolean | bool | logical Boolean (true/false) |
| box | rectangular box on a plane | |
| bytea | binary data ("byte array") | |
| character [ (n) ] | char [ (n) ] | fixed-length character string |
| character varying [ (n) ] | varchar [ (n) ] | variable-length character string |
| cidr | IPv4 or IPv6 network address | |
| circle | circle on a plane | |
| date | calendar date (year, month, day) | |
| double precision | float8 | double precision floating-point number (8 bytes) |
| inet | IPv4 or IPv6 host address | |
| integer | int, int4 | signed four-byte integer |
| interval [ fields ] [ (p) ] | time span | |
| json | textual JSON data | |
| jsonb | binary JSON data, decomposed | |
| line | infinite line on a plane | |
| lseg | line segment on a plane | |
| macaddr | MAC (Media Access Control) address | |
| money | currency amount | |
| numeric [ (p, s) ] | decimal [ (p, s) ] | exact numeric of selectable precision |
| path | geometric path on a plane | |
| pg_lsn | PostgreSQL Log Sequence Number | |
| point | geometric point on a plane | |
| polygon | closed geometric path on a plane | |
| real | float4 | single precision floating-point number (4 bytes) |
| smallint | int2 | signed two-byte integer |
| smallserial | serial2 | autoincrementing two-byte integer |
| serial | serial4 | autoincrementing four-byte integer |
| text | variable-length character string | |
| time [ (p) ] [ without time zone ] | time of day (no time zone) | |
| time [ (p) ] with time zone | timetz | time of day, including time zone |
| timestamp [ (p) ] [ without time zone ] | date and time (no time zone) | |
| timestamp [ (p) ] with time zone | timestamptz | date and time, including time zone |
| tsquery | text search query | |
| tsvector | text search document | |
| txid_snapshot | user-level transaction ID snapshot | |
| uuid | universally unique identifier | |
| xml | XML data |
SQL DDL Syntax (URL)
※ SQL은 대소문자를 구분짓지 않는다.
create database [DB_Name];
drop database [DB_Name];
SQL DML Syntax (URL)
※ SQL은 대소문자를 구분짓지 않는다.
1. Inserting Data (URL)
INSERT INTO table_name VALUES (values_list);
Example. Inserting Data
CREATE TABLE products (
product_no integer,
name text,
price numeric
);
INSERT INTO products VALUES (1, 'Cheese', 9.99);
-- Positional Association
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);
-- Named Association
INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
-- 일부 Field에 대한 값만 삽입하고자 하는 경우
INSERT INTO products VALUES (1, 'Cheese');
-- PostgreSQL Extension
-- 왼쪽 Field부터 값을 대입해나가며, 나머지 값은 Default로 설정한다.
INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
-- 일부 Field를 Default값으로 설정한다.
INSERT INTO products DEFAULT VALUES;
-- 모든 Field를 Default값으로 설정한다.
2. Updating Data (URL)
3. Deleting Data (URL)
4. Returning Data From Modified Rows (URL)
SQL DCL Syntax
※ SQL은 대소문자를 구분짓지 않는다.
PostgreSQL Homepage (URL)
