byteamagic

从 PostgreSQL bytea 值检测 MIME 类型与文件格式

概览

扩展包名版本分类许可证语言
pg_byteamagic0.2.4UTILBSD 2-ClauseC
ID扩展名BinLibLoadCreateTrustReloc模式
4275byteamagic-

Extension name is byteamagic; package name is pg_byteamagic.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.2.41817161514pg_byteamagic-
RPMPIGSTY0.2.41817161514pg_byteamagic_$v-
DEBPIGSTY0.2.41817161514postgresql-$v-pg-byteamagic-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
d12.aarch64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
d13.x86_64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
d13.aarch64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u22.x86_64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u22.aarch64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u24.x86_64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u24.aarch64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u26.x86_64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
u26.aarch64
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4
PIGSTY 0.2.4

构建

您可以使用 pig build 命令构建 pg_byteamagic 扩展的 RPM / DEB 包:

pig build pkg pg_byteamagic         # 构建 RPM / DEB 包

安装

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

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

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

pig install pg_byteamagic;          # 当前活跃 PG 版本安装
pig ext install -y pg_byteamagic -v 18  # PG 18
pig ext install -y pg_byteamagic -v 17  # PG 17
pig ext install -y pg_byteamagic -v 16  # PG 16
pig ext install -y pg_byteamagic -v 15  # PG 15
pig ext install -y pg_byteamagic -v 14  # PG 14
dnf install -y pg_byteamagic_18       # PG 18
dnf install -y pg_byteamagic_17       # PG 17
dnf install -y pg_byteamagic_16       # PG 16
dnf install -y pg_byteamagic_15       # PG 15
dnf install -y pg_byteamagic_14       # PG 14
apt install -y postgresql-18-pg-byteamagic   # PG 18
apt install -y postgresql-17-pg-byteamagic   # PG 17
apt install -y postgresql-16-pg-byteamagic   # PG 16
apt install -y postgresql-15-pg-byteamagic   # PG 15
apt install -y postgresql-14-pg-byteamagic   # PG 14

创建扩展

CREATE EXTENSION byteamagic;

用法

来源: official repo, official doc

byteamagic 会对 bytea 值运行 libmagic,因此 PostgreSQL 可以识别数据库中 blob 的 MIME 类型或人类可读文件类型。包名是 pg_byteamagic,但扩展名是 byteamagic

CREATE EXTENSION byteamagic;

SELECT byteamagic_mime(data) FROM files;
SELECT byteamagic_text(data) FROM files;

函数

  • byteamagic_mime(bytea) returns text: 返回 MIME 类型,对应 file --mime-type
  • byteamagic_text(bytea) returns text: 返回描述性的文件类型文本,对应 file

常见用法

SELECT
  id,
  byteamagic_mime(blob) AS mime,
  byteamagic_text(blob) AS kind
FROM uploads;

注意事项

  • 它只检查 bytea 内容;没有运算符、自定义类型或额外的 SQL 管理对象。
  • 构建或安装需要 PostgreSQL 开发头文件,以及系统 libmagic 开发包。
  • 上游文档非常简略;当前面向用户的行为与长期存在的文档页一致。

最后修改 2026-05-01: update extension data (e399d22)