General options: -c, --command=COMMAND run only single command (SQL or internal) and exit -d, --dbname=DBNAME database name to connect to (default: "postgres") -f, --file=FILENAME execute commands from file, thenexit -l, --list list available databases, thenexit -v, --set=, --variable=NAME=VALUE set psql variable NAME to VALUE (e.g., -v ON_ERROR_STOP=1) -V, --version output version information, thenexit -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, thenexit --help=commands list backslash commands, thenexit --help=variables list special variables, thenexit
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: "postgres") -w, --no-password never prompt for password -W, --password force password prompt (should happen automatically)
dropTABLE IF EXISTS company; CREATE TABLE IF NOTEXISTS company ( id serial PRIMARY KEY, title CHAR(50) UNIQUENOT NULL, subtitle varchar(500) UNIQUENOT NULL, age INTNOT NULLDEFAULT0, address TEXT, salary REAL, money decimal, status double precision, guid uuid ); -- \d+ company insert into company(name,address,age,salary) values('dennis','成都','28',3.14); insert into company(name,address,age,salary) values('dennis1','成都','28',3.14); insert into company(name,address,age,salary) values('dennis2','成都','28',3.14); insert into company(name,address,age,salary) values('dennis3','成都','28',3.14); insert into company(name,address,age,salary) values('dennis4','成都','28',3.14); insert into company(name,address,age,salary) values('dennis5','成都','28',3.14); deletefrom company where age=28;
postgresql、mysql数据类型比较
postgresql
mysql
无
TINYINT
smallint(2字节)
SMALLINT(2字节)
无
MEDIUMINT(3字节)
integer(4字节)
INT或INTEGER(4字节)
bigint(8字节)
BIGINT(8字节)
decimal(可变长)
DECIMAL
numeric(可变长)
无
real(4字节)
FLOAT(4字节)
double(8字节)
DOUBLE(8字节)
character varying(n), varchar(n)
CHAR(0-255 bytes)
character(n), char(n)
VARCHAR(0-65535 byte)
无
TINYTEXT
text(无长度限制)
TEXT(0-65 535 bytes)
无
MEDIUMTEXT(0-16 777 215 bytes)
无
LONGTEXT(0-4 294 967 295 bytes)
timestamp(8字节)
TIMESTAMP(4字节)
date(4字节)
DATE(3字节)
time(8字节)
TIME(0-4 294 967 295 bytes)
interval(12字节)
无
smallserial(2字节)
无
serial(4字节)
无
bigserial(8字节)
无
json、jsonb数据类型
二者的区别在于json写入快,读取慢,jsonb写入慢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
drop table if exists json_test; CREATE TABLE json_test ( id serial, board_id float NOT NULL, data jsonb ); INSERT INTO json_test VALUES (1, 1, '{"name": "Paint house", "tags": ["Improvements", "Office"], "finished": true}'); INSERT INTO json_test VALUES (2, 1, '{"name": "Wash dishes", "tags": ["Clean", "Kitchen"], "finished": false}'); INSERT INTO json_test VALUES (3, 1, '{"name": "Cook lunch", "tags": ["Cook", "Kitchen", "Tacos"], "ingredients": ["Tortillas", "Guacamole"], "finished": false}'); INSERT INTO json_test VALUES (4, 1, '{"name": "Vacuum", "tags": ["Clean", "Bedroom", "Office"], "finished": false}'); INSERT INTO json_test VALUES (5, 1, '{"name": "Hang paintings", "tags": ["Improvements", "Office"], "finished": false}'); SELECT data->>'name' AS name FROM json_test; SELECT * FROM json_test WHERE data->>'finished' = 'true';
insert into json_test select * from json_test;
vacuum full json_test
->将以JSON对象的形式返回该属性,而->>将以整数或文本
数组类型
在存储一些数据时能更节省空间
1 2 3 4 5 6 7 8 9 10 11
drop table if exists array_test; CREATE TABLE array_test ( id serial primary key, data int[] ); INSERT INTO array_test VALUES (1,'{3,5}'); select * from array_test; update array_test set data[0]=3434 where id=1; select * from array_test; update array_test set data[7]=3434 where id=1; select * from array_test;
SELECT procpid,START,now() -STARTAS lap,current_query FROM(SELECT backendid,pg_stat_get_backend_pid(S.backendid) AS procpid,pg_stat_get_backend_activity_start (S.backendid) ASSTART,pg_stat_get_backend_activity (S.backendid) AS current_query FROM(SELECT pg_stat_get_backend_idset () AS backendid) AS S) AS S WHERE current_query <>'<IDLE>'ORDERBY lap DESC;
配置相关接口
1 2 3 4 5
select name,setting from pg_settings where category='File Locations'; select name,setting from pg_settings; altersystemset log_statement='none'; SELECT pg_reload_conf(); show log_statement;
upsert
1
insert into test values (1,'test',now()) on conflict(id) do updateset info=excluded.info,crt_time=excluded.crt_time;
select*from pg_statio_user_indexes; -- 查询索引内存命中率 select relname,relname,idx_blks_read,idx_blks_hit,idx_blks_hit*100/(idx_blks_read+idx_blks_hit) as idx_hit_rate from pg_statio_user_indexes;
select*from pg_statio_user_tables; -- 查询索引内存命中率 select heap_blks_read,heap_blks_hit,idx_blks_read,idx_blks_hit,heap_blks_hit*100/(heap_blks_hit+heap_blks_read) as heap_hit_rate,idx_blks_hit*100/(idx_blks_read+idx_blks_hit) as idx_hit_rate from pg_statio_user_tables;