---
title: Encrypt data using pgcrypto | Tiger Data Docs
description: Encrypt sensitive data in your PostgreSQL database using the pgcrypto extension
---

The `pgcrypto` PostgreSQL extension provides cryptographic functions such as:

- General hashing
- Password hashing
- PGP encryption
- Raw encryption
- Random-data

For more information about these functions and the options available, see the [pgcrypto documentation](https://www.postgresql.org/docs/current/pgcrypto.html).

## Use the `pgcrypto` extension to encrypt inserted data

The `pgcrypto` extension allows you to encrypt, decrypt, hash, and create digital signatures within your database. Tiger Data understands how precious your data is and safeguards sensitive information.

1. **Install the `pgcrypto` extension**

   ```
   CREATE EXTENSION IF NOT EXISTS pgcrypto;
   ```

   Confirm the extension is installed using the `\dx` command:

   ```
       List of installed extensions
           Name         | Version |   Schema   |                                      Description
   ---------------------+---------+------------+---------------------------------------------------------------------------------------
    pg_stat_statements  | 1.10    | public     | track planning and execution statistics of all SQL statements executed
    pgcrypto            | 1.3     | public     | cryptographic functions
    plpgsql             | 1.0     | pg_catalog | PL/pgSQL procedural language
    timescaledb         | 2.24.0  | public     | Enables scalable inserts and complex queries for time-series data (Community Edition)
    timescaledb_toolkit | 1.22.0  | public     | Library of analytical hyperfunctions, time-series pipelining, and other SQL utilities
   ```

2. **Create a table named `user_passwords`**

   ```
   CREATE TABLE user_passwords (username varchar(100) PRIMARY KEY, crypttext text);
   ```

3. **Insert values and replace `<Password_Key>` with a password key of your choice**

   ```
   INSERT INTO user_passwords (username, crypttext)
   VALUES ('user1', pgp_sym_encrypt('user1_password','<Password_Key>')),
          ('user2', pgp_sym_encrypt('user2_password','<Password_Key>'));
   ```

4. **Confirm the password is encrypted**

   ```
   SELECT * FROM user_passwords;
   ```

   The encrypted passwords are listed:

   ```
    username |                                                                             crypttext
   ----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------
    user1    | \xc30d0407030269b21a28c0c3123a6cd23f0116e86a28aeb3730d7940b30ead5753e6510264553a3b2a763ffed91f0ac07868660d04b3b61bf5ccff170cab3e5fb25de9b7d5c7dc3021061325be977ed0
    user2    | \xc30d040703027b2952e9170ce4a461d23f01c7c91ce5683e49767368670a79e028456a47f98e783eda0a9428807bcff5f0654a867eecc34031bf36555e3d6f44ffa331b6c9d36d42818ecdfa8b29713b
   (2 rows)
   ```

5. **View the decrypted passwords**

   Replace `<Password_Key>` with the password key that you created:

   ```
   SELECT username, pgp_sym_decrypt(crypttext::bytea, '<Password_Key>')
   FROM user_passwords;
   ```

   The decrypted passwords are listed:

   ```
    username | pgp_sym_decrypt
   ----------+-----------------
    user1    | user1_password
    user2    | user2_password
   (2 rows)
   ```
