1. 형식
[PGPASSWORD=_비밀번호_]
psql \\
-h **_호스트_** \\
-p **_포트_** \\
-d **_DB_** \\
-U **_사용자_** \\
-c "실행할 쿼리"
2. 예시
# 로컬호스트 연결
psql -U postgres
# 로컬호스트 쿼리 실행
psql -U root -d root -c "SELECT 1"
# 복합
# 호스트
# - [1] 로컬 컴퓨터의 호스트(Windows)
# - [2] [1]에서 도커로 올린 컨테이너 Postgresql
# - [3] 리모트 호스트의 Postgresql (RDS)
# 목표
# - [1]에 있는 csv 파일을 [3]의 테이블로 전송
# 방법
# - [3]을 [1]에 로컬 포워딩(:9432), [2]에서 [1]에 접근하여 결과적으로 [3]에 연결
# - [1]-[2]의 볼륨을 바인딩하여 [1]에 있는 csv 파일을 [3]에 \\copy를 통해 전송
PGPASSWORD=password \\
psql \\
-h host.docker.internal \\
-p 9432 \\
-d db_name \\
-U user_name -c \\
"\\copy schema_name.table_name from STDIN
WITH DELIMITER E'\\t' ENCODING 'UTF8'"\\ < /docker-entrypoint-initdb.d/__data__.csv
Windows Docker 명령어 예시
docker run ^
-p 15432:5432 ^
--name "pg-15_2-15432" ^
-e POSTGRES_USER=root ^
-e POSTGRES_PASSWORD=1234 ^
-v C:/docker-volumes/pg-15_2:/docker-entrypoint-initdb.d ^
-d postgres:15.2
참고
Usage:
psql [OPTION]... [DBNAME [USERNAME]]
General options:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-d, --dbname=DBNAME database name to connect to (default: "root")
-f, --file=FILENAME execute commands from file, then exit
-l, --list list available databases, then exit
-v, --set=, --variable=NAME=VALUE
set psql variable NAME to VALUE
(e.g., -v ON_ERROR_STOP=1)
-V, --version output version information, then exit
-X, --no-psqlrc do not read startup file (~/.psqlrc)
-1 ("one"), --single-transaction
execute as a single transaction (if non-interactive)
-?, --help[=options] show this help, then exit
--help=commands list backslash commands, then exit
--help=variables list special variables, then exit
Input and output options:
-a, --echo-all echo all input from script
-b, --echo-errors echo failed commands
-e, --echo-queries echo commands sent to server
-E, --echo-hidden display queries that internal commands generate
-L, --log-file=FILENAME send session log to file
-n, --no-readline disable enhanced command line editing (readline)
-o, --output=FILENAME send query results to file (or |pipe)
-q, --quiet run quietly (no messages, only query output)
-s, --single-step single-step mode (confirm each query)
-S, --single-line single-line mode (end of line terminates SQL command)
Output format options:
-A, --no-align unaligned table output mode
--csv CSV (Comma-Separated Values) table output mode
-F, --field-separator=STRING
field separator for unaligned output (default: "|")
-H, --html HTML table output mode
-P, --pset=VAR[=ARG] set printing option VAR to ARG (see \\pset command)
-R, --record-separator=STRING
record separator for unaligned output (default: newline)
-t, --tuples-only print rows only
-T, --table-attr=TEXT set HTML table tag attributes (e.g., width, border)
-x, --expanded turn on expanded table output
-z, --field-separator-zero
set field separator for unaligned output to zero byte
-0, --record-separator-zero
set record separator for unaligned output to zero byte
Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "root")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)