Postgresql - Client and Server Applications - Commands

本文全面介绍了PostgreSQL数据库的各种客户端和服务器应用程序,包括clusterdb、createdb、createuser、dropdb、dropuser等工具的详细用法,以及pg_basebackup、pg_dump、pg_dumpall等备份与恢复工具,还有pg_ctl、pg_resetwal、pg_rewind等服务器控制工具的使用说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OS: MacOS

Version: 11.1

 

 

Client Applications

clusterdb

作用:cluster a PostgreSQL database

用法1:clusterdb test # cluster 整库

用法2:clusterdb --table=foo test # cluster 库里的一个表

 

createdb

作用:create a new PostgreSQL database

用法:createdb -U dbadmin -T template0 -e demo --lc-collate=C --lc-ctype=C --owner=mytest

 

createuser

作用:define a new PostgreSQL user account

用法:createuser -h eden -p 5000 -S -D -R -e joe

 

dropdb

作用:remove a PostgreSQL database

用法:dropdb -U dbadmin demo

 

dropuser

作用:remove a PostgreSQL user account

用法:dropuser -U dbadmin mytest

 

ecpg

作用:embedded SQL C preprocessor

用法:ecpg prog1.pgc

 

pg_basebackup

作用: take a base backup of a PostgreSQL cluster

用法1:pg_basebackup -h 192.168.13.53 -U dbsr -F p -P -R -D /usr/local/pgsql/data

用法2:pg_basebackup -D backup -Ft -z -P

 

pg_bench

作用:run a benchmark test on PostgreSQL

用法:pgbench -i -U dbadmin -d db_test --foreign-keys --scale=100 --fillfactor=60

--------------------------------

dropping old tables...

NOTICE: table "pgbench_accounts" does not exist, skipping

NOTICE: table "pgbench_branches" does not exist, skipping

NOTICE: table "pgbench_history" does not exist, skipping

NOTICE: table "pgbench_tellers" does not exist, skipping

creating tables...

generating data...

100000 of 100000 tuples (100%) done (elapsed 0.16 s, remaining 0.00 s)

vacuuming...

creating primary keys...

done.

--------------------------------

 

pg_config

作用: retrieve information about the installed version of PostgreSQL

用法:pg_config --docdir --bindir --htmldir --pkgincludedir --includedir-server --libdir --pkglibdir --localedir --mandir --sharedir --sysconfdir --pgxs --configure --cc .......

 

pg_dump

作用: extract a PostgreSQL database into a script file or other archive file

用法1:pg_dump -Fc mydb -f db.dump

用法2:pg_dump -U dbadmin -d mydb -T 'test*' -f test.sql # 以

用法3:pg_dump -U dbadmin -d mydb -t test01 -f test01.sql

 

pg_dumpall

作用: extract a PostgreSQL database cluster into a script file

用法1:pg_dumpall -U dbadmin -f db.sql

用法2:pg_dumpall -U postgres -g -f golbal_data.sql

 

pg_isready

作用:check the connection status of a PostgreSQL server

用法:pg_isready -h host01 -p 5432

 

pg_receivewal

作用:stream write-ahead logs from a PostgreSQL server

说明:pg_receivewal用于流式传输正在运行的PostgreSQL集群中的提前写入日志。提前写入日志使用流式复制协议进行流式处理,并写入本地文件目录。此目录可用作使用时间点恢复进行恢复的存档位置。pg_receivewal实时地流式处理预写日志,因为它是在服务器上生成的,并且不像archive_命令那样等待段完成。因此,在使用pg_receivewal时,不需要设置存档超时。与PostgreSQL备用服务器的wal接收器不同,pg_receivewal默认情况下只在关闭wal文件时刷新wal数据。必须指定选项--synchronous以实时刷新wal数据。

用法:

 

pg_recvlogical

作用:control PostgreSQL logical decoding streams

说明:pg逻辑控制逻辑解码复制槽,并从这些复制槽流数据。

用法:

 

pg_restore

作用:restore a PostgreSQL database from an archive file created by pg_dump

用法:pg_restore -U dbadmin -h host -j 12 -Fc -d dbname dbname.dmp

 

pgbench

作用:

用法:pgbench -i -U dbadmin -d db_test --foreign-keys --scale=100 --fillfactor=60

--------------------------------

dropping old tables...

NOTICE: table "pgbench_accounts" does not exist, skipping

NOTICE: table "pgbench_branches" does not exist, skipping

NOTICE: table "pgbench_history" does not exist, skipping

NOTICE: table "pgbench_tellers" does not exist, skipping

creating tables...

generating data...

100000 of 100000 tuples (100%) done (elapsed 0.16 s, remaining 0.00 s)

vacuuming...

creating primary keys...

done.

--------------------------------

 

psql

作用:PostgreSQL interactive terminal

用法1:psql -U dbadmin -d postgres -h host -p 5432

 

reindexdb

作用: reindexdb is a utility for rebuilding indexes in a PostgreSQL database.

用法:reindexdb -U dbadmin -d mytest --table=test --index=test_id_idx

 

vacuumdb

作用:garbage-collect and analyze a PostgreSQL database

用法:vacuumdb --analyze --verbose --table='foo(bar)' xyzzy

 

 

oid2name

作用:resolve OIDs and file nodes in a PostgreSQL data directory

用法1:oid2name

用法2:oid2name -d mytest -t test01

 

vacuumlo

作用:remove orphaned large objects from a PostgreSQL database

说明:vacuumlo是一个简单的实用程序,可以从PostgreSQL数据库中删除任何“孤立”的大型对象。孤立大对象(LO)被认为是其OID未出现在数据库的任何OID或LO数据列中的任何LO。

用法:

 

 

Server Applications

initdb

作用:create a new PostgreSQL database cluster

用法:

 

pg_archivecleanup

作用:clean up PostgreSQL WAL archive files

说明:当作为备用服务器运行时,pg_archive clean up被设计成一个archive_clean up_命令来清理wal文件归档。pg_archivecleanup也可以作为一个独立的程序来清理wal文件档案。

要将备用服务器配置为使用pg_archivecleanup,放入recovery.conf配置文件。

用法1:pg_archivecleanup -d archive 000000010000003700000010.00000020.backup

用法2:archive_cleanup_command = 'pg_archivecleanup -d /mnt/standby/archive %r 2>>cleanup.log'

 

pg_controldata

作用: display control information of a PostgreSQL database cluster

用法:pg_controldata

 

pg_ctl

作用: initialize, start, stop, or control a PostgreSQL server

用法:

pg_ctl init[db] [-D datadir] [-s] [-o initdb-options]

pg_ctl start [-D datadir] [-l filename] [-W] [-t seconds] [-s] [-o options] [-p path] [-c]

pg_ctl stop [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ] [-W] [-t seconds] [-s]

pg_ctl restart [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ] [-W] [-t seconds] [-s] [-o options] [-c]

pg_ctl reload [-D datadir] [-s]

pg_ctl status [-D datadir]

pg_ctl promote [-D datadir] [-W] [-t seconds] [-s]

pg_ctl kill signal_name process_id

On Microsoft Windows, also:

pg_ctl register [-D datadir] [-N servicename] [-U username] [-P password] [-S a[uto] | d[emand] ] [-e source] [-W] [-t seconds] [-s] [-o options]

pg_ctl unregister [-N servicename]

 

pg_resetwal

作用: reset the write-ahead log and other control information of a PostgreSQL database cluster

说明:服务器运行时不得使用此命令。

用法:pg_resetwal -D /usr/local/pgsql/data/

 

pg_rewind

作用:synchronize a PostgreSQL data directory with another data directory that was forked from it

说明:pg_rewind是一个工具,用于在集群的时间线出现分歧后,将PostgreSQL集群与同一集群的另一个副本进行同步。一个典型的场景是,在故障转移之后,将旧的主服务器作为新主服务器之后的备用服务器重新联机。结果相当于用源数据目录替换目标数据目录。只复制来自关系文件的已更改块;所有其他文件(包括配置文件)都将被完整复制。与使用新的基本备份或rsync等工具相比,pg_rewind的优势在于pg_rewind不需要读取集群中未更改的块。这使得当数据库很大时速度更快,并且集群之间只有很小一部分块不同。pg_rewind检查源集群和目标集群的时间线历史,以确定它们的分歧点,并期望在目标集群的pg_wal目录中找到wal,一直延伸到分歧点。分歧点可以在目标时间线、源时间线或它们的共同祖先上找到。在典型的故障转移场景中,目标集群在分歧后很快关闭,这不是问题,但是如果目标集群在分歧后运行很长时间,那么旧的wal文件可能不再存在。在这种情况下,可以手动将它们从wal归档文件复制到pg_wal目录,或者通过配置recovery.conf在启动时获取。pg_w rewind的使用不仅限于故障转移,例如,可以升级备用服务器,运行一些写事务,然后重新倒带以再次成为备用服务器。当目标服务器在运行pg_rewind后第一次启动时,它将进入恢复模式,并在偏离点后重放源服务器中生成的所有wal。如果在运行pg_rewind时,源服务器中的某些wal不再可用,因此pg_rewind会话无法复制,则必须在启动目标服务器时使其可用。这可以通过使用合适的restore_command 在目标数据目录中创建recovery.conf文件来完成。pg_rewind要求目标服务器在postgresql.conf中启用wal_log_hints选项,或者在使用initdb初始化集群时启用数据校验和。默认情况下,这两个选项都当前处于打开状态。完整的页面写入也必须设置为打开,但默认情况下是启用的。

用法:pg_rewind --target-pgdata $PGDATA --source-server='host=db1 port=5432 user=postgres dbname=postgres' -P

 

pg_test_fsync

作用:determine fastest wal_sync_method for PostgreSQL

说明:了解特定系统上最快的wal-sync方法,并在识别出I/O问题时提供诊断信息。然而,pg_test_fsync 所显示的差异可能不会对实际数据库吞吐量产生任何显著的影响,特别是因为许多数据库服务器的速度不受其提前写入日志的限制。pg_test_fsync 以微秒为单位报告每个 wal_sync_method 的平均文件同步操作时间,这也可用于通知优化提交延迟值的工作。

用法:pg_test_fsync

 

pg_test_timing

作用:measure timing overhead

说明:pg_test_timing是一个工具,用于测量系统上的计时开销,并确认系统时间不会向后移动。收集定时数据速度较慢的系统可以给出不太准确的解释分析结果。

用法:pg_test_timing

 

pg_upgrade

作用:upgrade a PostgreSQL server instance

用法:

 

pg_verify_checksums

作用:verify data checksums in a PostgreSQL database cluster

说明:cluster must be shut down to verify checksums

用法:pg_verify_checksums

 

pg_waldump

作用:display a human-readable rendering of the write-ahead log of a PostgreSQL database cluster

用法1:pg_waldump -b 000000010000000100000030 000000010000000100000031

用法2:pg_waldump -b --start=1/304B2C20 --end=1/304B2DA8

用法3:pg_waldump -b -f -r Transaction 00000001000000010000003

 

postgres

作用:

用法:

 

postmaster

作用:

用法:

 

pg_standby

作用:supports the creation of a PostgreSQL warm standby server

用法:

 

 

### 不同操作系统上安装 PostgreSQL 客户端的方法 #### 1. **Linux** 在 Linux 上,可以根据不同的发行版选择合适的安装方式。 ##### (a) 基于 Red Hat 的系统(如 CentOS, RHEL) 可以通过 YUM 存储库安装 PostgreSQL 客户端。首先添加官方存储库: ```bash yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E '%{rhel}')-x86_64/pgdg-redhat-repo-latest.noarch.rpm ``` 接着安装客户端软件包: ```bash yum install -y postgresqlXX ``` 这里的 `XX` 是指代 PostgreSQL 的版本号,例如 `postgresql13` 表示安装 PostgreSQL 13 的客户端[^2]。 ##### (b) Debian/Ubuntu 系统 对于基于 Debian 的系统,可以使用 APT 包管理器进行安装。首先更新系统的包索引: ```bash sudo apt-get update ``` 然后安装 PostgreSQL 客户端: ```bash sudo apt-get install -y postgresql-client ``` 此命令会自动安装最新稳定版本的 PostgreSQL 客户端[^5]。 #### 2. **Windows** 在 Windows 平台上,通常通过图形化界面完成安装。访问 PostgreSQL 官方网站下载适用于 Windows 的二进制安装程序[^3]。虽然该安装向导默认提供完整的服务器和客户端组件,但如果仅需要客户端部分,可以选择自定义安装选项,并勾选必要的工具如 `psql` 和其他命令行实用程序。 另一种方法是从 EnterpriseDB 提供的链接下载独立的二进制压缩文件[^1],从中提取所需的客户端应用程序到本地路径即可使用。 #### 3. **macOS** Mac 用户有多种途径来获取 PostgreSQL 客户端支持: ##### 方法一:利用 Homebrew Homebrew 是 macOS 下非常流行的包管理工具。先确保已经安装好 Brew 后运行下面这条指令刷新可用资源列表: ```bash brew update ``` 之后执行以下语句拉取并部署最新的 PostgreSQL 客户端及相关依赖项: ```bash brew install libpq ``` 这样不仅能够获得基础的 psql 接口还能同步引入一些辅助功能模块[^5]。 ##### 方法二:直接运用 Postgres.app Postgres.app 是一款专为 Mac 设计的一键式解决方案,它集成了服务端与前端应用于一体。只需将其拖放到 Applications 文件夹里启动就能立即享受到便捷的服务体验。 --- ### 验证安装是否成功 无论在哪种平台上完成了上述任意一种形式的布置工作后都建议测试一下基本的功能运转状况。打开终端窗口输入如下命令查看当前所处环境下的 PostgreSQL 版本信息以及确认能否正常调用交互式的 SQL shell ——即所谓的 'psql' 工具。 ```bash psql --version ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值