PostgreSQL工具杂记

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 工作中会用到很多小工具,特此记录。批量导出表只导出insert语句pg_dump -h host -p 5432 -U postgres -a -t t1 -t t2 --inserts -f /opt/temp.

工作中会用到很多小工具,特此记录。

批量导出表

只导出insert语句
pg_dump -h host -p 5432 -U postgres -a -t t1 -t t2 --inserts -f /opt/temp.sql -d mcsas
导出全部表结构和模式
pg_dump -h host -p 5432 -U postgres -t t1 -t t2 -f /opt/temp.sql -d mcsas

数据库备份

pg_dump -h master -p 5432-U postgres -w -f /home/postgres/test.backup test

数据库还原

pg_restore -h master -p 5432-U postgres -w -d test /home/postgres/test.backup
如果是文本格式的dump,直接使用
psql的 \i xxx.backup

shp导入pg

使用pgadmin3的可视化工具,也可以通过命令行
用法:shp2pgsql [<options>] <shapefile> [[<schema>.]<table>]
OPTIONS:
-s : [<from>:]<srid> 设置 SRID字段,默认0,不能和 -D一起使用。
(-d|a|c|p) 常用的互斥操作选项
-d 删除之前的表,重建一个表导入shp数据。
-a 将shp数据追加到已有的表,在同一个schema下。
-c 创建一个新表,然后导入shp数据,不指定操作选项会默认这个。
-p 预备模式,只创建表,不导入数据。
-g <geocolumn> 指定表的图形列,(更多用在append模式下)
-D Use postgresql dump format (defaults to SQL insert statements).
-e 独立执行,不使用事务。.和-D不相容。
-G Use geography type (requires lon/lat data or -s to reproject).
-k Keep postgresql identifiers case.
-i 对dbf中所有的integer子弹使用int4类型
-I 对geocolumn创建空间索引。
-m <filename> Specify a file containing a set of mappings of (long) column
names to 10 character DBF column names. The content of the file is one or
more lines of two names separated by white space and no trailing or
leading space. For example:
COLUMNNAME DBFFIELD1
AVERYLONGCOLUMNNAME DBFFIELD2
-S 创建单图形而不是MULTI 图形。
-t <dimensionality> 强制指定图形为 '2D', '3DZ', '3DM', or '4D'
-w Output WKT instead of WKB. Note that this can result in
coordinate drift.
-W <encoding> Specify the character encoding of Shape's
attribute column. (default: "UTF-8")
-N <policy> 空图形策略 (insert*,skip,abort).
-n 只导入DBF文件
-T <tablespace> Specify the tablespace for the new table.
Note that indexes will still use the default tablespace unless the
-X flag is also used.
-X <tablespace> Specify the tablespace for the table's indexes.
This applies to the primary key, and the spatial index if
the -I flag is used.
-? Display this help screen.
举例如下,导入一个shp,指定geomcolumn名称为geom,建立空间字段,图形类型是单义图形。
shp2pgsql -c -g geom -D -s 4326 -S -i -I shaperoads.shp myschema.roadstable | psql -d roadsdb

pg导出shp

用法: pgsql2shp [<options>] <database> [<schema>.]<table>
pgsql2shp [<options>] <database> <query>

OPTIONS:
-f <filename> 导出文件名称
-h <host> 数据库host
-p <port> 数据库port
-P <password> 指定密码
-u <user> 指定用户
-g <geometry_column> 指定输出geom列名称
-b Use a binary cursor.
-r Raw mode. Do not assume table has been created by the loader. This would
not unescape attribute names and will not skip the 'gid' attribute.
-k Keep PostgreSQL identifiers case.
-m <filename> Specify a file containing a set of mappings of (long) column
names to 10 character DBF column names. The content of the file is one or
more lines of two names separated by white space and no trailing or
leading space. For example:
COLUMNNAME DBFFIELD1
AVERYLONGCOLUMNNAME DBFFIELD2
-? Display this help screen.

举例如下,将testdb数据中public的schema中test表导出为shp。
pgsql2shp -h host -p 5432 -u postgres -f /opt/test.shp testdb public.test

数据库备库

pg_basebackup -D $PGDATA -Fp -Xs -v -P -h master -p 5432 -U repuser

数据库同步时间线

pg_rewind --target-pgdata=/home/postgres/data --source-server='host=slave port=5432 user=postgres dbname=postgres'

删除表重复数据
DELETE FROM weather 
WHERE ctid 
NOT IN (
SELECT max(ctid) 
FROM weather 
GROUP BY city, temp_lo, temp_hi, prcp, date
);
跨表更新

update test t1 set field1=t2.field1 from test2 t2 where t1.id=t2.id

新建事务临时表

create temp table tt(id int,name text) on commit drop; --事务结束就消失
create temp table tt(id int,name text) on commit delete rows; --事务结束数据消失
create temp table tt(id int,name text) on commit preserver rows; --数据存在整个会话周期中

赋予用户读取schema权限
alter default privileges in schema public grant all on tables to freerep;
alter default privileges in schema public revoke all on tables to freerep;
pgpool强制由master执行sql
/*NO LOAD BALANCE*/ select * from abc;
强制断开所有连接
select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='tt';
copy导入csv
copy sexit from 'e:/sexit.csv' delimiter as '|' csv quote as '''';
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
4月前
|
关系型数据库 Java 数据库连接
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
164 3
|
4月前
|
SQL 关系型数据库 MySQL
postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用
postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用
222 0
|
11月前
|
监控 关系型数据库 Go
《打造高可用PostgreSQL:策略与工具》
《打造高可用PostgreSQL:策略与工具》
178 0
|
4月前
|
关系型数据库 测试技术 数据库
`pg_rewind` 是 PostgreSQL 数据库的一个工具,用于将一个数据库集群回退到指定的时间点
pg_rewind 是 PostgreSQL 数据库的一个工具,用于将一个数据库集群回退到指定的时间点。这对于恢复数据或解决某些问题非常有用。 简单来说,如果你有一个 PostgreSQL 数据库集群并且你知道在某个时间点它是健康的,但之后出现了问题,你可以使用 pg_rewind 来将数据库回退到那个时间点,从而恢复到已知的、健康的、一致的状态。 使用 pg_rewind 的基本步骤如下: 确定基准时间:首先,你需要确定一个基准时间点,知道在该时间点上数据库是健康的。 备份当前数据库:在执行 pg_rewind 之前,确保你已经备份了当前的数据库。 执行 pg_rewind:使用
147 1
|
10月前
|
SQL JSON 关系型数据库
PostgreSQL技术大讲堂 - 第34讲:调优工具pgBagder部署
PostgreSQL从小白到专家技术大讲堂 - 第34讲:调优工具pgBagder部署
1178 1
|
4月前
|
关系型数据库 数据库 PostgreSQL
postgresql|数据库迁移|ora2pg工具的web界面介绍
postgresql|数据库迁移|ora2pg工具的web界面介绍
109 0
|
4月前
|
SQL 关系型数据库 测试技术
postgresql|数据库|数据库测试工具pgbench之使用
postgresql|数据库|数据库测试工具pgbench之使用
192 0
|
数据可视化 关系型数据库 数据库
2023年三个最佳的免费PostgreSQL GUI工具
2023年三个最佳的免费PostgreSQL GUI工具
901 0
2023年三个最佳的免费PostgreSQL GUI工具
|
11月前
|
SQL 监控 关系型数据库
Navicat 面向 PostgreSQL 查询超时的工具解决方案
Navicat 面向 PostgreSQL 查询超时的工具解决方案
149 0
|
SQL Oracle 关系型数据库
【postgreSQL】psql工具特有的快捷命令
【postgreSQL】psql工具特有的快捷命令
8871 1