Overview of pgcrypto in PostgreSQL

In a world of increasing privacy regulations (like GDPR, HIPAA) and rising cyberattacks, encrypting sensitive data is no longer optional. PostgreSQL, known for its robustness and extensibility, comes with a powerful cryptographic extension called pgcrypto. This guide will walk you through what pgcrypto is, how it works, and how to implement secure encryption and hashing directly within your PostgreSQL database.

What is pgcrypto?

pgcrypto is a PostgreSQL extension that provides cryptographic functions directly within SQL. It allows you to perform:

* Secure password hashing (with bcrypt, SHA256, etc.)

* Symmetric encryption/decryption (with passphrases)

* Asymmetric (public/private key) encryption

* Data integrity verification (digests)

* Random byte generation for tokens and salts

Because it's native to PostgreSQL, it avoids round-tripping to application code and keeps cryptographic operations close to the data.

Why Use pgcrypto?

Use CaseFeatureBenefit

Password storage

crypt() + gen_salt()

Salted and hashed passwords (bcrypt, etc.)

PII encryption (e.g. SSNs)

pgp_sym_encrypt() / decrypt

AES-like symmetric encryption

Token or key generation

gen_random_bytes()

Secure session or API token generation

Message integrity

digest()

Generate SHA or MD5 hashes

PGP-style encryption

pgp_pub_encrypt() / decrypt

Asymmetric key support (public/private keys)

Getting Started

1. Enable the Extension

CREATE EXTENSION IF NOT EXISTS pgcrypto;

Make sure your PostgreSQL version supports it (virtually all do since 9.1+).

2. Create a Secure Users Table

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username TEXT UNIQUE NOT NULL,
    password_hash TEXT NOT NULL
);

Password Hashing with crypt() and bcrypt

Step 1: Store a Hashed Password

INSERT INTO users (username, password_hash)
VALUES (
    'admin',
    crypt('mysecretpassword', gen_salt('bf'))
);

* 'bf' means bcrypt, a slow hashing algorithm ideal for password security.

* gen_salt('bf') generates a unique salt every time.

Step 2: Verify a Password at Login

SELECT crypt('mysecretpassword', password_hash) = password_hash AS is_match
FROM users
WHERE username = 'admin';

If is_match returns true, the password is correct.

Symmetric Encryption with pgp_sym_encrypt()

This is useful when you need to encrypt and decrypt data using a shared secret key.

Encrypting a Value

SELECT pgp_sym_encrypt('Sensitive Data', 'myencryptionkey');

Decrypting It Later

SELECT pgp_sym_decrypt(encrypted_column, 'myencryptionkey');

Don't store myencryptionkey inside your database. Use environment variables or a secrets manager.

Asymmetric (Public/Private Key) Encryption

This is helpful when you want to separate the roles of encryption and decryption.

Encrypt with Public Key

SELECT pgp_pub_encrypt('Top Secret', dearmor(public_key_column))
FROM keys WHERE user_id = 1;

Decrypt with Private Key

SELECT pgp_priv_decrypt(encrypted_column, dearmor(private_key_column), 'keypassphrase')
FROM keys WHERE user_id = 1;

Hashing Arbitrary Data

If you want to hash any string (e.g., for file checksums, integrity verification):

SELECT encode(digest('hello world', 'sha256'), 'hex');

Supported algorithms: md5, sha1, sha224, sha256, sha384, sha512

Generate Random Bytes

Need tokens or unique salts?

SELECT encode(gen_random_bytes(16), 'hex');  -- 32-char hex string

Best Practices

TipWhy it Matters

Use bcrypt over md5 or sha for passwords

Slow, adaptive hashing defends against brute-force

Don’t hardcode encryption keys in SQL

Reduces risk if the DB is compromised

Regularly rotate encryption keys

Limits damage from leaked keys

Enable SSL for connections

Protects data in transit

Use pgcrypto only where encryption is essential

Keeps queries readable and maintainable

Real-World Applications

* Secure email verification tokens

* Encrypting salary or tax IDs in HR systems

* Logging hashed audit data (integrity checks)

* Building user authentication into internal tools

* Odoo modules with encrypted fields using pgcrypto

Further Reading

* PostgreSQL pgcrypto Docs

* OWASP Password Storage Cheat Sheet

* GnuPG for public/private key generation

Conclusion

pgcrypto turns PostgreSQL into a cryptographic powerhouse. With its ability to handle password hashing, symmetric and asymmetric encryption, and random data generation, it provides the essential tools you need to keep data secure, right inside your database.

Whether you're building login systems, storing PII, or adding encryption to your Odoo modules, mastering pgcrypto means adding a strong layer of security without leaving SQL. Explore how PostgreSQL MVCC Internals Affect Performance by delving into the mechanics of Multi-Version Concurrency Control. This article explains how MVCC manages data versions, enables high concurrency without blocking, and how crucial processes like VACUUM are for maintaining optimal database performance and preventing bloat.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message