pg_pwhash

PostgreSQL 高级密码哈希扩展(Argon2/scrypt/yescrypt)

概览

扩展包名版本分类许可证语言
pg_pwhash1.0SECMITC
ID扩展名BinLibLoadCreateTrustReloc模式
7330pg_pwhash-

RPM metadata shows license=PostgreSQL, but packaged LICENSE file is MIT

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.01817161514pg_pwhash-
RPMPGDG1.01817161514pg_pwhash_$v-
DEBPGDG1.01817161514postgresql-$v-pg-pwhash-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d12.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d13.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
d13.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u22.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u22.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u24.x86_64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
u24.aarch64
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0
PGDG 1.0

安装

您可以直接安装 pg_pwhash 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_pwhash;          # 当前活跃 PG 版本安装
pig ext install -y pg_pwhash -v 18  # PG 18
pig ext install -y pg_pwhash -v 17  # PG 17
pig ext install -y pg_pwhash -v 16  # PG 16
pig ext install -y pg_pwhash -v 15  # PG 15
pig ext install -y pg_pwhash -v 14  # PG 14
dnf install -y pg_pwhash_18       # PG 18
dnf install -y pg_pwhash_17       # PG 17
dnf install -y pg_pwhash_16       # PG 16
dnf install -y pg_pwhash_15       # PG 15
dnf install -y pg_pwhash_14       # PG 14
apt install -y postgresql-18-pg-pwhash   # PG 18
apt install -y postgresql-17-pg-pwhash   # PG 17
apt install -y postgresql-16-pg-pwhash   # PG 16
apt install -y postgresql-15-pg-pwhash   # PG 15
apt install -y postgresql-14-pg-pwhash   # PG 14

创建扩展

CREATE EXTENSION pg_pwhash;

用法

pg_pwhash: PostgreSQL 高级密码哈希

pg_pwhash 为 PostgreSQL 提供现代自适应密码哈希算法,包括 Argon2、scrypt 和 yescrypt。

CREATE EXTENSION pg_pwhash;

支持的算法

标识符算法Salt 模式
argon2iArgon2i$argon2i$v=19$m=4096,t=3,p=1$<salt>
argon2dArgon2d$argon2d$v=19$m=4096,t=3,p=1$<salt>
argon2idArgon2id$argon2id$v=19$m=4096,t=3,p=1$<salt>
scryptScrypt$scrypt$ln=16,r=8,p=1$<salt>
$7$Scrypt (crypt)$7$BU<salt>
yescryptyescrypt (crypt)$y$j9T$<salt>

核心函数

生成盐值和哈希

-- Argon2id(推荐)
SELECT pwhash_crypt('password', pwhash_gen_salt('argon2id'));
-- $argon2id$v=19$m=4096,t=3,p=1$<salt>$<hash>

-- Scrypt
SELECT pwhash_crypt('password', pwhash_gen_salt('scrypt'));

-- Yescrypt
SELECT pwhash_crypt('password', pwhash_gen_salt('yescrypt'));

验证密码

-- 如果输出等于存储的哈希值则匹配
SELECT stored_hash = pwhash_crypt('entered_password', stored_hash) AS valid;

直接哈希函数

SELECT pwhash_argon2('password', pwhash_gen_salt('argon2id'));
SELECT pwhash_scrypt('password', pwhash_gen_salt('scrypt'));
SELECT pwhash_yescrypt_crypt('password', pwhash_gen_salt('yescrypt'));

自定义盐值参数

-- 自定义内存/时间/并行度的 Argon2
SELECT pwhash_gen_salt('argon2id', 'm=65536', 't=4', 'p=2');

-- 自定义参数的 Scrypt
SELECT pwhash_gen_salt('scrypt', 'ln=20', 'r=8', 'p=1');

配置

参数描述
pg_pwhash.argon2_default_backendArgon2 后端:libargon2openssl

最后修改 2026-03-14: update extension metadata (953cbd0)