
// 설치
brew install postgresql
// 서비스 시작
pg_ctl -D /usr/local/var/postgres start && brew services start postgresql
// 실행 확인
postgres -V
// 접속
psql postgres [-U username] [-h host] [-p port] [-d database]
// user 권한 확인
postgres=# \du
// password 설정
postgres=# \password [username]
// 새 권한 생성 및 권한 부여
postgres=# CREATE ROLE [rolename] WITH LOGIN PASSWORD '[password]';
postgres=# ALTER ROLE [rolename] CREATEDB;
// 새 유저 생성
postgres=# createuser [username] --createdb
// 새 데이터베이스 생성
postgres=# CREATE DATABASE [db name];
postgres=# createdb [db name] [-O ownername];
// 데이터베이스 owner 변경
postgres=# ALTER DATABASE [db name] OWNER TO [username];
// 접근가능한 유저 추가, 권한 설정
postgres=# GRANT ALL PRIVILEGES ON DATABASE [dbname] TO [username];
psql로 접속시 default는 다음과 같다.
user : 현재 사용자이름
host : localhost
port : 5432
database: 현재 사용자명과 동일한 이름의 DB
| command | 설명 |
|---|---|
| \list(or \l) | 전체 DB 인스턴스 목록 |
| \dt | 접속한 DB의 테이블 목록 |
| \ds | Sequence 목록 |
| \df | Function 목록 |
| \dv | View 목록 |
| \du | User 목록 |
외에도 \dn(Schema 목록), \di(Index 목록) 등이 있다.
\?로 추가 명령어 조회가 가능하다. 또한 {command}+를 붙이면 상세정보(Description)가 추가된다.
상세정보 조회
\d [tablename] column, type, collation, nullable, default 등의 정보가 조회
Command History 조회 및 활용
\g 최근에 실행한 명령어 실행
\s 이전에 실행한 명령어 전체 조회
도움말
\h SQL command 도움말
\? psql command 도움말
Query 결과 display 설정
\x Column이 길어 한줄 조회가 힘들 때 세로배치 on/off
\a Column Align on/off (default on)
\H 결과를 HTML tabel형식으로 display on/off
Query 실행시간
\timing 실행시간 on/off. Query tuning 등 실행시간 확인에 사용
외부 파일을 통한 Query 실행
\i [file name] 길고 복잡한 쿼리 등 외부 편집기에서 작성한 파일을 가져다 실행
ex. \i test.sql
외부 에디터 사용
\e [file name] \i는 외부에디터에서 작성'된' 쿼리를 가져오고, \e는 외부 편집기를 통해 쿼리를 작성'해서' 실행. Linux는 vi, Window는 notepad가 Default Editor
\ef [function name] \e 와 비슷하나 function을 편집할 때 사용함
file name 생략 시 buffer를 통해 작성되고, 실행 후 작성한 쿼리는 사라진다.
shell command 실행
\! \i, \e 등을 위해 현재 경로에 있는 파일을 조회할 때나 path 변경에 사용.
ex.
\! clear : 화면 clear
\! pwd : 현재 경로 확인
\! ls : 현재 경로 파일 확인
\! cd [Path] : path로 이동
psql 종료
\q
| Name | Aliases | 설명 |
|---|---|---|
| Bigint | int8 | signed 8byte 정수형 |
| Bigserial | serial8 | 자동증가 8byte 정수형 |
| bit[(n)] | 고정길이 bit string | |
| bit varying[(n)] | Varbit | 가변길이 bit string |
| Boolean | Bool | Boolean연산 (true/false) |
| Box | 평면 위의 직사각형 상자 | |
| Bytea | 이진 data ("byte array") | |
| character [(n)] | char[(n)] | 고정길이 character string |
| character varying [(n)] | varchar[(n)] | 가변길이 character string |
| Cidr | IPv4 or IPv6 network address | |
| Circle | 평면 위의 면 | |
| Date | 달력 날짜 (year, month, day) | |
| double precision | float8 | double precision floating-point number (8bytes) |
| Inet | IPv4, IPv6 host address | |
| Integer | int, int4 | signed four-byte 정수형 |
| interval [fields] [(p)] | time 구간 | |
| Json | 문자 JSON data | |
| Jsonb | 이진 JSON data, decomposed | |
| Macaddr | MAC(Media Access Control) address | |
| Money | 현금 총액 | |
| pg_lsn | PostgreSQL Log 번호 | |
| Real | float4 | single precision floating-point number |
| Smallint | int2 | signed 2bytes 정수형 |
| Smallserial | serial2 | 자동증가 2bytes 정수형 |
| Serial | serial4 | 자동증가 4bytes 정수형 |
| Text | 가변길이 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] | 날짜와 시간 (no time zone) | |
| timestamp[(p)] with time zone | timestamptz | 날짜와 시간, including time zone |
| Tsquery | text 검색 쿼리 | |
| Tsvector | text 검색 문서 | |
| Xml | XML data |
https://www.postgresql.org/docs/9.5/reference-client.html
https://semtax.tistory.com/12
https://browndwarf.tistory.com/51
https://folivora.tistory.com/127
https://hochoon-dev.tistory.com/entry/PostgreSQL-%EA%B8%B0%EB%B3%B8-%EC%82%AC%EC%9A%A9%EB%B2%952
유용한 정보 감사합니다. ! clear 찾아서 한참 헤맸어요 ㅜㅡㅜ