Skip to content
Gzip compress and decompress for PostgreSQL
C Makefile
Branch: master
Clone or download
Latest commit 35f9cca Nov 13, 2019

PostgreSQL gzip/gunzip Functions

Build Status


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.


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


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;


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

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


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');
 test text

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

 this text has been compressed and then decompressed


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



If you have PostgreSQL devel packages and zlib installed, you should have pg_config on your path, so you should be able to just run make, then make install, then in your database CREATE EXTENSION gzip.

If your libz is installed in a non-standard location, you may need to edit ZLIB_PATH in the Makefile.

You can’t perform that action at this time.