Supabase Postgres extensions are just PostgreSQL extensions, but they’re managed by Supabase.
This is how you enable pgcrypto and uuid-ossp in your Supabase project.
Enabling Extensions
You can enable extensions through the Supabase dashboard or by running SQL commands directly.
Via Supabase Dashboard
- Navigate to your project’s dashboard.
- Go to the Database section.
- Select Extensions.
- Find
pgcryptoanduuid-osspin the list. - Click the Install button next to each extension.
Via SQL Commands
Connect to your Supabase database using a SQL client (like Supabase CLI’s psql command, DBeaver, or pgAdmin). Then, run the following commands:
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
The IF NOT EXISTS clause prevents errors if the extension is already enabled.
What are these extensions for?
pgcrypto
This extension provides cryptographic functions for hashing passwords, encrypting data, and generating random data.
Common Use Cases:
- Password Hashing: Securely store user passwords.
- Data Encryption: Encrypt sensitive columns in your database.
- Random Data Generation: Generate random strings or numbers.
Example: Hashing a Password
SELECT crypt('mysecretpassword', gen_salt('bf'));
This command generates a salt and hashes the password using the Blowfish algorithm. The output will look something like $2a$12$0s0W3rE5tYuIoPdFgHjKlMnOpQrStUvWxYzAbCdEfGhIjKlM. You would store this hash in your database.
To verify a password:
SELECT crypt('mysecretpassword', '$2a$12$0s0W3rE5tYuIoPdFgHjKlMnOpQrStUvWxYzAbCdEfGhIjKlM');
-- This will return 'mysecretpassword' if it matches, or NULL if it doesn't.
uuid-ossp
This extension provides functions for generating Universally Unique Identifiers (UUIDs). UUIDs are 128-bit values that are unique across space and time.
Common Use Cases:
- Primary Keys: Use UUIDs as primary keys for your tables, which can be beneficial for distributed systems or when you need to generate IDs client-side.
- Unique Identifiers: Generate unique IDs for various purposes within your application.
Example: Generating UUIDs
-- Generate a random UUID (version 4)
SELECT uuid_generate_v4();
-- Generate a UUID based on timestamp and MAC address (version 1)
SELECT uuid_generate_v1();
The output of uuid_generate_v4() will be a string like a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11.
Integrating with Supabase Tables
You can use these extensions to define default values for your table columns.
Example: Using uuid-ossp for a primary key
When creating a users table:
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL
);
Now, every time a new row is inserted into the users table without an explicit id provided, a new UUID will be automatically generated for it.
Example: Using pgcrypto for password hashing on insert
You could use a trigger to automatically hash passwords.
CREATE OR REPLACE FUNCTION hash_user_password()
RETURNS TRIGGER AS $$
BEGIN
NEW.password_hash := crypt(NEW.password_hash, gen_salt('bf'));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER hash_password_trigger
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE FUNCTION hash_user_password();
With this trigger, when you INSERT a new user with their plain-text password in the password_hash column, it will be automatically hashed before being stored.
The next challenge is often managing these extensions across different environments (dev, staging, prod).