ps aux | grep postgres
使用命令登录数据库psql -U postgres -d testdb -h localhost -p 5432
postgres用户名,testdb数据库名称,localhost ip地址,可以省略,5432端口,可以省略。
\h :查看 SQL 命令的解释,比如 \h select 。
\? :查看 psql 命令列表。
\l :列出所有数据库。
\c [database_name] :连接其他数据库。
\d :列出当前数据库的所有表格。
\d [table_name] :列出某一张表格的结构。
\du :列出所有用户。
\e :打开文本编辑器。
\conninfo :列出当前数据库和连接的信息\、
select version();--查询postgresql版本
SELECT PostGIS_full_version(); --查询postgis版本
SHOW data_directory;-- 查询数据文件安装路径
select pg_conf_load_time(); -- 查询加载配置时间
select pg_xlog_location_diff(pg_current_xlog_insert_location(),pg_current_xlog_location()); --查看当前wal的buffer中有多少字节没有写入到磁盘中
--- 备注:需要开启pg_stat_statements
select * from pg_stat_statements order by total_time desc limit 5; -- 查询最耗时的5个sql
-- 获取执行时间最慢的3条SQL,并给出CPU占用比例
SELECT substring(query, 1, 1000) AS short_query,
round(total_time::numeric, 2) AS total_time,
calls,
round((100 * total_time / sum(total_time::numeric) OVER ())::numeric, 2) AS percentage_cpu
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 3;
EXPLAIN ANALYZE SELECT * FROM product; -- 分析sqk执行情况
查看当前长时间执行却不结束的SQL
select datname, usename, client_addr, application_name, state, backend_start, xact_start, xact_stay, query_start, query_stay, replace(query, chr(10), ' ') as query from (select pgsa.datname as datname, pgsa.usename as usename, pgsa.client_addr client_addr, pgsa.application_name as application_name, pgsa.state as state, pgsa.backend_start as backend_start, pgsa.xact_start as xact_start, extract(epoch from (now() - pgsa.xact_start)) as xact_stay, pgsa.query_start as query_start, extract(epoch from (now() - pgsa.query_start)) as query_stay , pgsa.query as query from pg_stat_activity as pgsa where pgsa.state != 'idle' and pgsa.state != 'idle in transaction' and pgsa.state != 'idle in transaction (aborted)') idleconnections order by query_stay desc limit 5;
查出使用表扫描最多的表
select * from pg_stat_user_tables where n_live_tup > 100000 and seq_scan > 0 order by seq_tup_read desc limit 10;
查询读取buffer最多的5个SQL
select * from pg_stat_statements order by shared_blks_hit+shared_blks_read desc limit 5;
获取数据库当前的回滚事务数以及死锁数
select datname,xact_rollback,deadlocks from pg_stat_database
查询指定表的慢查询
select * from pg_stat_activity where query ilike '%%' and query_start - now() > interval '10 seconds';
select count(*) from pg_stat_activity; -- 查询数据库链接数量
show max_connections; -- 获取数据库最大连接数
select * from pg_stat_activity; -- 当前链接数详细信息
select usename, count(*) from pg_stat_activity group by usename; -- 查询数据库中各个用户名对应的数据库连接数
创建数据库与授权
---------------------------------建库基本流程----------------------------------------------------
create user eas PASSWORD 'eas_Ytkj2023'; --创建用户eas 密码为eas_Ytkj@2023
CREATE DATABASE xiyimpaf; --创建数据库
create schema ebms; --创建schema
grant all on database xiyimpaf to eas; --数据库授权 给eas
grant select on all tables in schema public to eas; -- table和schema授权给eas
--------------------------------------------------------------------------------------
设置某个(些)表的Owner身份
ALTER TABLE tb_menu OWNER TO jtzq; -- 设置tb_menu所属owner 给jtzq,解决删除报错问题
grant all privileges on table product to username --- 授予予指定用户username指定表product的所有权限
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO username; -- 授予指定用户所有表的所有权限
--将所有表owner权限给jtzq
SELECT
'Alter table ' || d.relname || ' owner to jtzq;'
FROM
(
SELECT C
.relname
FROM
pg_class C
WHERE
C.relname IN ( SELECT tablename FROM pg_tables WHERE tablename NOT LIKE'pg%' AND tablename NOT LIKE'sql_%' and tablename not like 'spatial%' )
ORDER BY
C.relname )d
添加字段
alter table user_role(表命) add column createby(字段名) varchar(50) ; --创建名
comment on column user_role.createby is '创建人';
alter table user_role add column createtime timestamp(0);
comment on column user_role.createtime is '创建时间';
alter table user_role add column updateby varchar(50) ;
comment on column user_role.updateby is '更新人';
alter table user_role add column updatetime timestamp(0) ;
comment on column user_role.updatetime is '更新时间';
用户查询
SELECT * FROM pg_roles;--查看所有已有角色及其权限(角色就是用户)
SELECT * FROM pg_user;
CREATE ROLE tigger LOGIN inherit; -- 创建角色 tigger,并设置权限(参考其他库的用户权限)
select * from pg_tables; -- 获取数据库中所有表
-- 查询数据库中所有表及其描述
select relname as TABLE_NAME ,col_description(c.oid, 0) as COMMENTS from pg_class c where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%';
索引
select * from pg_indexes where tablename = 'product'; -- 获取表product的所有索引
查询
select name from pg_available_extensions; -- 查询那些扩展可用
select * from pg_extension; -- 查询已经打开的扩展
添加
// 添加空间数据库的相关插件
CREATE EXTENSION postgis; //用于创建空间数据库的插件,仅支持矢量数据扩展
CREATE EXTENSION address_standardizer; //使空间数据库支持地址标准化
CREATE EXTENSION ogr_fdw; //使空间数据库支持不同数据库之间的跨库操作
CREATE EXTENSION pgrouting; //使空间数据库支持网络分析
CREATE EXTENSION pointcloud; //使空间数据库支持点云数据存储
CREATE EXTENSION pointcloud_postgis; //使空间数据库支持点云数据操作
CREATE EXTENSION postgis_raster; //使空间数据库支持栅格数据扩展
CREATE EXTENSION postgis_sfcgal; //使空间数据库支持2D和3D的数据操作
CREATE EXTENSION fuzzystrmatch; //使空间数据库支持地理编码的模糊匹配
CREATE EXTENSION postgis_tiger_geocoder; //使空间数据库支持地理编码
CREATE EXTENSION postgis_topology; //使空间数据库支持拓扑检查
删除
DROP extension postgis cascade;
select pg_size_pretty (pg_database_size('db_product')); -- 查询执行数据库大小
select datname, pg_size_pretty (pg_database_size(datname)) AS size from pg_database; -- 查询数据库实例中各个数据库大小
select pg_size_pretty(pg_relation_size('geo_wind')) as size; -- 查询表达数据大小
select pg_size_pretty(pg_indexes_size('geo_wind')); --查询表中索引大小
-- 获取各个表中的数据记录数
select relname as TABLE_NAME, reltuples as rowCounts from pg_class where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%'; order by rowCounts desc;
select pg_relation_filepath('geo_wind'); -- 查看数据库表对应的数据文件
查询表的坐标系
select * from geometry_columns where f_table_name in('jsline','matrix'); --jsline,matrix是表名
select st_srid(geom) from jsline --获取表的空间srid
update t_noisemap set geometry =ST_SetSRID(geometry, 0) --更新空间参考
select updategeometrysrid('jsline','geom',900913); --更新表空间参考
1、备份postgres库并tar打包
pg_dump -h 127.0.0.1 -p 5432 -U postgres -f postgres.sql.tar -Ft
2、备份postgres库,转储数据为带列名的INSERT命令
pg_dumpall -d postgres -U postgres -f postgres.sql --column-inserts
1、备份postgres库并tar打包
pg_dump -h 127.0.0.1 -p 5432 -U postgres -f postgres.sql.tar -Ft
2、备份postgres库,转储数据为带列名的INSERT命令
pg_dumpall -d postgres -U postgres -f postgres.sql --column-inserts