select version();
show server_version;
show server_version_num;
xtestdb=# select version();
PostgreSQL 9.4.26 (Greenplum Database 6.19.0 build commit:c88801925e5d51e4be57f54ff2158ed74a264b8e Open Source) on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 6.4.0, 64-bit compiled o
n Dec 8 2021 23:08:44
(1 row)
testdb=# show server_version;
server_version
----------------
9.4.26
(1 row)
testdb=# show server_version_num;
server_version_num
--------------------
90426
(1 row)
xxxxxxxxxx
testdb=# create database info_user;
CREATE DATABASE
xxxxxxxxxx
testdb=# alter database info_user rename to user_info;
ALTER DATABASE
xxxxxxxxxx
testdb=# alter database user_info connection limit 150;
ALTER DATABASE
xxxxxxxxxx
testdb=# drop database if exists user_info;
xxxxxxxxxx
-- \l
testdb=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+------------+------------+---------------------
dingyj | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
dmcpgsdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
template1 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
testdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
user_info | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
(7 rows)
xxxxxxxxxx
-- \c
testdb=# \c dmcpgsdb
You are now connected to database "dmcpgsdb" as user "gpadmin".
dmcpgsdb=#
xxxxxxxxxx
-- \dn
dmcpgsdb=# \dn
List of schemas
Name | Owner
----------------------+---------
dmcpgsdb_read_schema | gpadmin
gp_toolkit | gpadmin
public | gpadmin
(3 rows)
xxxxxxxxxx
-- \du
dmcpgsdb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------+-----------
dingyj | | {}
gpadmin | Superuser,Ext gpfdist Table, Wri Ext gpfdist Table | {}
testro | | {}
wangt | | {}
xxxxxxxxxx
dmcpgsdb=# \dp ada_n042_fda_punish
Access privileges
Schema | Name | Type | Access privileges | Column access privileges
--------+---------------------+-------+-------------------------+--------------------------
public | ada_n042_fda_punish | table | gpadmin=arwdDxt/gpadmin+|
| | | wangt=r/gpadmin |
(1 row)
不加表名则显示全部
xxxxxxxxxx
dmcpgsdb-# \x
Expanded display is on.
dmcpgsdb=# select rec_id,inv from amr_n042_company_inv limit 2;
-[ RECORD 1 ]------------
rec_id | 683cdf2dda657390
inv | 黄秀英
-[ RECORD 2 ]------------
rec_id | 3be2c6a663402c80
inv | 马美霞
-- 当不想列展示时,再次\x即可
dmcpgsdb=# \x
Expanded display is off.
dmcpgsdb=# select rec_id,inv from amr_n042_company_inv limit 2;
rec_id | inv
------------------+--------
73749551b10d5efc | 田英
47a370afaa75b84b | 李挪英
(2 rows)
xxxxxxxxxx
dmcpgsdb=# \d amr_n042_company_inv
Table "public.amr_n042_company_inv"
Column | Type | Modifiers
-----------------+------------------------+-----------
rec_id | character varying(64) |
eid | character varying(64) |
inv | character varying(300) |
pid | character varying(64) |
eid_inv | character varying(64) |
inv_type | character varying(50) |
blic_type | character varying(50) |
blic_no | text |
country | character varying(50) |
currency | character varying(50) |
sub_conam | numeric(38,6) |
acc_conam | numeric(38,6) |
sub_conam_usd | numeric(38,6) |
acc_conam_usd | numeric(38,6) |
con_prop | numeric(38,6) |
sharehd_num | bigint |
is_listed | integer |
con_form | text |
con_date | character varying(10) |
anche_year_date | character varying(30) |
rnk_conp | integer |
update_time | character varying(30) |
delete_flg | character(1) |
Indexes:
"ix_amr_n042_company_inv_eid" btree (eid)
"ix_amr_n042_company_inv_eid_inv" btree (eid_inv)
"ix_amr_n042_company_inv_pid" btree (pid)
Distributed by: (rec_id)
xxxxxxxxxx
-- \o 指定本地环境路径
dmcpgsdb=# \o /home/gpadmin/company_inv.txt
-- 执行需要导出数据的SQL
dmcpgsdb=# select rec_id,inv from amr_n042_company_inv limit 5;
-- 查看验证
gpadmin@gpmaster:/home/gpadmin >ll /home/gpadmin/company_inv.txt
-rw-rw-r-- 1 gpadmin gpadmin 213 Dec 27 17:04 /home/gpadmin/company_inv.txt
gpadmin@gpmaster:/home/gpadmin >cat /home/gpadmin/company_inv.txt
rec_id | inv
------------------+--------
683cdf2dda657390 | 黄秀英
3be2c6a663402c80 | 马美霞
604c930953200df1 | 张彦岗
3be3ca37602a0e19 | 王惠
52b9b19298f5a1a4 | 黄小红
(5 rows)
xxxxxxxxxx
dmcpgsdb=# select datid, datname, pid, client_addr, client_port, query_start, backend_start from pg_stat_activity;
datid | datname | pid | client_addr | client_port | query_start | backend_start
-------+----------+--------+-------------+-------------+-------------------------------+-------------------------------
16385 | dmcpgsdb | 4948 | 11.8.88.88 | 22019 | 2022-12-27 16:57:29.790592+08 | 2022-12-27 16:57:29.775261+08
16385 | dmcpgsdb | 5188 | 11.8.88.88 | 63291 | 2022-12-27 16:59:52.481708+08 | 2022-12-27 16:59:52.465111+08
16385 | dmcpgsdb | 5331 | 11.8.88.88 | 22281 | 2022-12-27 17:01:13.920819+08 | 2022-12-27 17:01:13.904222+08
16385 | dmcpgsdb | 3714 | 11.8.88.88 | 21093 | 2022-12-27 16:45:10.809441+08 | 2022-12-27 16:45:10.7932+08
16385 | dmcpgsdb | 4724 | 11.8.88.88 | 57582 | 2022-12-27 16:55:17.496382+08 | 2022-12-27 16:55:17.479785+08
16385 | dmcpgsdb | 4376 | 11.8.88.88 | 57411 | 2022-12-27 16:51:44.541171+08 | 2022-12-27 16:51:44.52488+08
16385 | dmcpgsdb | 4437 | 11.8.88.88 | 57443 | 2022-12-27 16:52:24.104285+08 | 2022-12-27 16:52:24.088149+08
16385 | dmcpgsdb | 4100 | 11.8.88.88 | 57294 | 2022-12-27 16:49:08.820976+08 | 2022-12-27 16:49:08.805455+08
查看数据库连接进程 每个服务器进程一行,显示数据库OID、数据库名、进程ID、客户端地址、客户端端口、最近查询时间、进程开始时间
xxxxxxxxxx
testdb=# select usename from pg_user;
usename
---------
gpadmin
dingyj
(2 rows)
注意: 创建的用户不赋予权限时,默认无任何操作权限
xxxxxxxxxx
testdb=# CREATE USER wangt WITH PASSWORD 'wangt_666';
gpadmin@gpmaster:/home/gpadmin >psql -U wangt -d dmcpgsdb -h 11.8.8.8 -W
Password for user wangt:
psql (9.4.26)
Type "help" for help.
dmcpgsdb=> select rec_id,inv,update_time from amr_n042_company_inv limit 1;
ERROR: permission denied for relation amr_n042_company_inv
xxxxxxxxxx
-- 用户以上方创建的wangt为例
-- 切换至需要赋权操作的数据库
testdb=# \c dmcpgsdb
You are now connected to database "dmcpgsdb" as user "gpadmin".
-- 设置为只读的transaction(设置用户默认开启只读事务)
dmcpgsdb=# alter user wangt set default_transaction_read_only = on;
ALTER ROLE
-- 创建SCHEMA
dmcpgsdb=# create schema dmcpgsdb_read_schema;
CREATE SCHEMA
-- 默认在postgres数据库的public模式下的对象是可以访问的:
-- 首先要有使用schema的权限:
dmcpgsdb=# grant usage on schema dmcpgsdb_read_schema to wangt;
GRANT
-- 然后加所有表的只读权限:
dmcpgsdb=# grant select on all tables in schema public to wangt;
GRANT
-- 使用普通用户登录访问验证
gpadmin@gpmaster:/home/gpadmin >psql -U wangt -d dmcpgsdb -h 11.8.8.8 -W
Password for user wangt:
psql (9.4.26)
Type "help" for help.
-- 可以正常查询数据
dmcpgsdb=> select rec_id,inv,update_time from amr_n042_company_inv limit 1;
rec_id | inv | update_time
------------------+--------+---------------------
683cdf2dda657390 | 黄秀英 | 2022-10-03 07:31:53
(1 row)
-- 建表语句没有权限
dmcpgsdb=> create table testaaa (id int);
ERROR: transaction is read-only
-- 删表没有权限
dmcpgsdb=> drop table amr_n042_company_inv;
ERROR: transaction is read-only
-- 表数据操作没有权限
dmcpgsdb=> insert into amr_n042_company_inv select * from amr_n042_company_inv limit 1;
ERROR: permission denied for relation amr_n042_company_inv
示例:
xxxxxxxxxx
gpadmin@gpmaster:/home/gpadmin >psql
psql (9.4.26)
Type "help" for help.
testdb=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+------------+------------+---------------------
dingyj | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
dmcpgsdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
template1 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
testdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
(6 rows)
testdb=#
gpadmin@gpmaster:/home/gpadmin >psql -E
psql (9.4.26)
Type "help" for help.
testdb=# \l
********* QUERY **********
SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
d.datcollate as "Collate",
d.datctype as "Ctype",
pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+---------+----------+------------+------------+---------------------
dingyj | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
dmcpgsdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
template1 | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 | =c/gpadmin +
| | | | | gpadmin=CTc/gpadmin
testdb | gpadmin | UTF8 | en_US.utf8 | en_US.utf8 |
(6 rows)
xxxxxxxxxx
\! clear
xxxxxxxxxx
\watch
示例:
xxxxxxxxxx
testdb=# \watch select NOW();
Watch every 1s Mon Dec 26 15:01:18 2022
now
-------------------------------
2022-12-26 15:01:18.908637+08
(1 row)
Watch every 1s Mon Dec 26 15:01:19 2022
now
-------------------------------
2022-12-26 15:01:19.912506+08
(1 row)
Watch every 1s Mon Dec 26 15:01:20 2022
now
-------------------------------
2022-12-26 15:01:20.915686+08
(1 row)
xxxxxxxxxx
\i filename
示例:
xxxxxxxxxx
gpadmin@gpmaster:/home/gpadmin >cat test.sql
select * from amr_n042_company_inv limit 1
gpadmin@gpmaster:/home/gpadmin >psql
psql (9.4.26)
Type "help" for help.
testdb=# \c dmcpgsdb
You are now connected to database "dmcpgsdb" as user "gpadmin".
dmcpgsdb=# \i test.sql
rec_id | eid | inv | pid | eid_inv | inv_type | blic_type | blic_no | country | currency | sub_conam | acc_conam | sub_conam_usd
| acc_conam_usd | con_prop | sharehd_num | is_listed | con_form | con_date | anche_year_date | rnk_conp | update_time | delete_flg
------------------+---------------------+------+----------------------------------+---------+----------+-----------+---------+---------+----------+------------+------------+---------------
+---------------+-----------+-------------+-----------+----------+----------+-----------------+----------+---------------------+------------
73749551b10d5efc | 1914401017640250210 | 田英 | 82546bfffff972f6cb478d002b27b250 | | 20 | | 20 | 156 | 156 | 275.000000 | 275.000000 |
| | 50.000000 | | 0 | 货币 | | | 1 | 2022-08-17 06:29:55 | 0
(1 row)
xxxxxxxxxx
-- ALTER DATABASE database_name SET statement_timeout = '60s';
-- 示例:
ALTER DATABASE dmcpgsdb SET statement_timeout = '60s';
设置SQL语句的执行时间过长运行则会阻止任务运行
xxxxxxxxxx
SELECT count(*), state
FROM pg_stat_activity
GROUP BY state;
count | state
-------+---------------------
1 | idle in transaction
1 | active
32 | idle
(3 rows)
idle in transaction 事务中空闲连接
active 活跃连接
idle 空闲连接
xxxxxxxxxx
-- 字节显示 SELECT pg_relation_size('table_name');
-- 示例:
SELECT pg_relation_size('amr_n042_company_inv');
pg_relation_size
------------------
30689853440
(1 row)
-- 显示格式(合理的文件大小单位) SELECT pg_size_pretty(pg_relation_size('table_name'));
-- 示例:
SELECT pg_size_pretty(pg_relation_size('amr_n042_company_inv'));
pg_size_pretty
----------------
29 GB
(1 row)
xxxxxxxxxx
SELECT relname AS relation,
pg_size_pretty (
pg_total_relation_size (C .oid)
) AS total_size
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C .relnamespace)
WHERE nspname NOT IN (
'pg_catalog',
'information_schema'
)
AND C .relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size (C .oid) DESC
LIMIT 10;
relation | total_size
---------------------------------------+------------
amr_n042_company_ar_capital | 65 GB
app_ent_region_nic_rank | 60 GB
amr_n042_company_manager | 53 GB
amr_n042_company_modify | 52 GB
amr_n042_company_inv | 46 GB
amr_n042_company_ar_socialfee | 45 GB
amr_n042_company_ar | 31 GB
taj_n042_court_execute | 16 GB
amr_n042_company_ar_modify | 11 GB
app_ent_financial_var_nic_rank_latest | 9661 MB
(10 rows)
不加limit则列出全部从大到小占用的表清单排列
xxxxxxxxxx
ALTER database postgres SET log_min_duration_statement = '250ms';
执行时间等于或者大于 log_min_duration_statement 设置值的参数时,sql语句会被记录到日志
xxxxxxxxxx
SELECT schemaname || '.' || relname AS table, indexrelname AS index
, pg_size_pretty(pg_relation_size(i.indexrelid)) AS "index size", idx_scan AS "index scans"
FROM pg_stat_user_indexes ui
JOIN pg_index i ON ui.indexrelid = i.indexrelid
WHERE NOT indisunique
AND idx_scan < 50
AND pg_relation_size(relid) > 5 * 8192
ORDER BY pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST, pg_relation_size(i.indexrelid) DESC
LIMIT 10;
table | index | index size | index scans
--------------------------------------+--------------------------------------+------------+-------------
public.amr_n042_company_ar_capital | ix_amr_n042_company_ar_capital_eid | 14 GB | 0
public.amr_n042_company_modify | ix_amr_n042_company_modify_eid | 11 GB | 0
public.amr_n042_company_manager | ix_amr_n042_company_manager_pid | 11 GB | 0
public.app_ent_region_nic_rank | ix_app_ent_region_nic_rank_eid | 10 GB | 0
public.amr_n042_company_ar | ix_amr_n042_company_ar_eid | 9618 MB | 0
public.amr_n042_company_ar_socialfee | ix_amr_n042_company_ar_socialfee_eid | 8179 MB | 0
public.amr_n042_company_manager | ix_amr_n042_company_manager_eid | 7925 MB | 0
public.amr_n042_company_inv | ix_amr_n042_company_inv_pid | 7890 MB | 0
public.amr_n042_company_inv | ix_amr_n042_company_inv_eid | 5991 MB | 0
public.amr_n042_company_inv | ix_amr_n042_company_inv_eid_inv | 3504 MB | 0
(10 rows)
按照索引大小降序排列,列出全部去掉limit限制
xxxxxxxxxx
SELECT reltuples::numeric as count
FROM pg_class
WHERE relname='table_name';
-- 示例:
SELECT reltuples::numeric as count
FROM pg_class
WHERE relname='amr_n042_company_inv';
count
-----------
156361000
(1 row)
select count(*) from amr_n042_company_inv;
count
-----------
156352907
(1 row)
对于上亿的数据量,统计出的结果仅仅0.005%误差,所以对于精度要求不是严格必须准确1条无误的情况下,可以使用评估方式,结果无耗时,count相对来说耗时耗性能
评估表中的数据量基于内部统计信息返回表中的近似数据量
xxxxxxxxxx
\timing
示例:
xxxxxxxxxx
-- 普通查询
dmcpgsdb=# select count(*) from amr_n042_company_ar;
count
-----------
251031650
(1 row)
-- 通过\timing开启计时功能
dmcpgsdb=# \timing
Timing is on.
-- 计时查询效果
dmcpgsdb=# select count(*) from amr_n042_company_ar;
count
-----------
251031650
(1 row)
Time: 42631.350 ms
xxxxxxxxxx
\x auto
示例:
xxxxxxxxxx
-- 普通查询
dmcpgsdb=# select * from amr_n042_company_inv limit 1;
rec_id | eid | inv | pid | eid_inv | inv_type | blic_type | blic_no | country | currency | sub_conam | acc_conam | sub_conam_usd
| acc_conam_usd | con_prop | sharehd_num | is_listed | con_form | con_date | anche_year_date | rnk_conp | update_time | delete_flg
------------------+---------------------+--------+----------------------------------+---------+----------+-----------+---------+---------+----------+-----------+-----------+---------------
+---------------+-----------+-------------+-----------+----------+------------+-----------------+----------+---------------------+------------
f349085e85b3daff | 1931505225641865849 | 张连财 | b69edd6c64d55e5d74c0645d98fa0b00 | | 20 | | | 156 | 156 | 20.000000 | 0.000000 | 0.000000
| 0.000000 | 10.000000 | | 0 | 实物 | 2010-11-17 | | 2 | 2022-09-29 07:34:05 | 0
(1 row)
-- 通过\x auto开启格式化查询结果
dmcpgsdb=# \x auto
Expanded display is used automatically.
-- 计格式化查询效果
dmcpgsdb=# select * from amr_n042_company_inv limit 1;
-[ RECORD 1 ]---+---------------------------------
rec_id | f349085e85b3daff
eid | 1931505225641865849
inv | 张连财
pid | b69edd6c64d55e5d74c0645d98fa0b00
eid_inv |
inv_type | 20
blic_type |
blic_no |
country | 156
currency | 156
sub_conam | 20.000000
acc_conam | 0.000000
sub_conam_usd | 0.000000
acc_conam_usd | 0.000000
con_prop | 10.000000
sharehd_num |
is_listed | 0
con_form | 实物
con_date | 2010-11-17
anche_year_date |
rnk_conp | 2
update_time | 2022-09-29 07:34:05
delete_flg | 0
xxxxxxxxxx
\e
示例:
xxxxxxxxxx
-- 例如平时SQL语句输入错误时,一般会光标按上找出上一句SQL修改,但是当SQL语句非常长时,使用\e编辑非常直观易用
dmcpgsdb=# select * from amr_n042_company_inv limitttt 1;
ERROR: syntax error at or near "1"
LINE 1: select * from amr_n042_company_inv limitttt 1;
^
-- 进入末次SQL编辑界面,:q退出或者:wq均可触发sql执行
dmcpgsdb=# \e
-[ RECORD 1 ]---+---------------------------------
rec_id | 683cdf2dda657390
eid | 1933506273156624479
inv | 黄秀英
pid | 74a9dfb2c89d54373e04c63031fa75ce
eid_inv |
inv_type | 20
blic_type |
blic_no | 20
country |
currency | 156
sub_conam | 40.000000
acc_conam | 0.000000
sub_conam_usd | 5.900000
acc_conam_usd |
con_prop | 5.000000
sharehd_num |
is_listed | 0
con_form | 货币
con_date | 2019-10-11
anche_year_date |
rnk_conp | 2
update_time | 2022-10-03 07:31:53
delete_flg | 0
xxxxxxxxxx
\pset null ⛔
符号自定义,根据需求定义即可。
示例:
xxxxxxxxxx
-- 普通查询
dmcpgsdb=# select * from amr_n042_company_inv limit 1;
rec_id | 683cdf2dda657390
eid | 1933506273156624479
inv | 黄秀英
pid | 74a9dfb2c89d54373e04c63031fa75ce
eid_inv |
inv_type | 20
blic_type |
blic_no | 20
country |
currency | 156
sub_conam | 40.000000
acc_conam | 0.000000
sub_conam_usd | 5.900000
acc_conam_usd |
con_prop | 5.000000
sharehd_num |
is_listed | 0
con_form | 货币
con_date | 2019-10-11
anche_year_date |
rnk_conp | 2
update_time | 2022-10-03 07:31:53
delete_flg | 0
-- 开启置换
dmcpgsdb=# \pset null ⛔
Null display is "⛔".
-- 验证效果
dmcpgsdb=# select * from amr_n042_company_inv limit 1;
rec_id | 73749551b10d5efc
eid | 1914401017640250210
inv | 田英
pid | 82546bfffff972f6cb478d002b27b250
eid_inv | ⛔
inv_type | 20
blic_type | ⛔
blic_no | 20
country | 156
currency | 156
sub_conam | 275.000000
acc_conam | 275.000000
sub_conam_usd | ⛔
acc_conam_usd | ⛔
con_prop | 50.000000
sharehd_num | ⛔
is_listed | 0
con_form | 货币
con_date | ⛔
anche_year_date | ⛔
rnk_conp | 1
update_time | 2022-08-17 06:29:55
delete_flg | 0