gzip

gzip and gunzip functions.

Overview

PackageVersionCategoryLicenseLanguage
pg_gzip1.0.0UTILMITC
IDExtensionBinLibLoadCreateTrustRelocSchema
4010gzipNoYesNoYesNoYes-
Relatedbzip zstd http pg_net pg_curl pgjq pgjwt pg_smtp_client

Version

TypeRepoVersionPG VerPackageDeps
EXTMIXED1.0.01817161514pg_gzip-
RPMPGDG1.0.01817161514pg_gzip_$v-
DEBPIGSTY1.0.01817161514postgresql-$v-gzip-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
u24.x86_64
u24.aarch64
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0
PIGSTY 1.0.0

Build

You can build the RPM / DEB packages for pg_gzip using pig build:

pig build pkg pg_gzip         # build RPM / DEB packages

Install

You can install pg_gzip directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_gzip;          # Install for current active PG version
pig ext install -y pg_gzip -v 18  # PG 18
pig ext install -y pg_gzip -v 17  # PG 17
pig ext install -y pg_gzip -v 16  # PG 16
pig ext install -y pg_gzip -v 15  # PG 15
pig ext install -y pg_gzip -v 14  # PG 14
dnf install -y pg_gzip_18       # PG 18
dnf install -y pg_gzip_17       # PG 17
dnf install -y pg_gzip_16       # PG 16
dnf install -y pg_gzip_15       # PG 15
dnf install -y pg_gzip_14       # PG 14
apt install -y postgresql-18-gzip   # PG 18
apt install -y postgresql-17-gzip   # PG 17
apt install -y postgresql-16-gzip   # PG 16
apt install -y postgresql-15-gzip   # PG 15
apt install -y postgresql-14-gzip   # PG 14

Create Extension:

CREATE EXTENSION gzip;

Usage

Sometimes you just need to compress your bytea object before you return it to the client.

Sometimes you receive a compressed bytea from the client, and you have to uncompress it before you can work with it.

This extension is for that.

This extension is not for storage compression. PostgreSQL already does tuple compression on the fly if your tuple gets large enough, manually pre-compressing your data using this function won’t make things smaller.

  • gzip(uncompressed BYTEA, [compression_level INTEGER]) returns BYTEA
  • gzip(uncompressed TEXT, [compression_level INTEGER]) returns BYTEA
  • gunzip(compressed BYTEA) returns BYTEA

Examples

> SELECT gzip('this is my this is my this is my this is my text');

                                   gzip
--------------------------------------------------------------------------
 \x1f8b08000000000000132bc9c82c5600a2dc4a851282ccd48a12002e7a22ff30000000

Wait, what, the compressed output is longer?!? No, it only looks that way, because in hex every byte is represented with two hex digits. The original string looks like this in hex:

> SELECT 'this is my this is my this is my this is my text'::bytea;

                                               bytea
----------------------------------------------------------------------------------------------------
 \x74686973206973206d792074686973206973206d792074686973206973206d792074686973206973206d792074657874

For really long, repetitive things, compression naturally works like a charm:

> SELECT gzip(repeat('this is my ', 100));

                                               bytea
----------------------------------------------------------------------------------------------------
 \x1f8b08000000000000132bc9c82c5600a2dc4a859251e628739439ca24970900d1341c5c4c040000

To convert a bytea back into an equivalent text you must use the encode() function with the escape encoding.

> SELECT encode('test text'::bytea, 'escape');
   encode
-----------
 test text

> SELECT encode(gunzip(gzip('this text has been compressed and then decompressed')), 'escape')

                      encode
-----------------------------------------------------
 this text has been compressed and then decompressed

Last Modified 2026-03-12: add pg extension catalog (95749bf)