Supabase Backend as a Service · Lezione

Utilizzo delle estensioni di Postgres

Scopra come abilitare e utilizzare potenti estensioni di PostgreSQL per aggiungere funzionalità come la generazione di UUID o la ricerca full-text

Lezione 1 di 311 passaggi

Utilizzo delle estensioni di Postgres è una lezione Supabase Backend as a Service gratuita su CoddyKit. Questa è la lezione 1 di 3. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Supabase Backend as a Service, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What are Postgres Extensions?

PostgreSQL is super powerful, but did you know you can make it even better? That's where extensions come in!

Extensions are like plugins for your database. They add new functions, data types, or operators that aren't available by default.

Supabase, built on PostgreSQL, lets you easily enable many of these extensions to boost your app's capabilities.

Enabling Extensions in Supabase

Turning on an extension in Supabase is straightforward. You usually do it through the SQL Editor in your Supabase Dashboard.

  • Go to the SQL Editor.
  • Run the command: CREATE EXTENSION extension_name;
  • Make sure you have admin privileges for your database.

Once enabled, the new features become available for use across your database.

CREATE EXTENSION "uuid-ossp"; -- Example for UUIDs

Unique IDs with `uuid-ossp`

Generating truly unique identifiers is crucial for many applications. PostgreSQL's built-in SERIAL or BIGINT sequences are great for simple auto-incrementing IDs, but they are sequential.

For global uniqueness, especially in distributed systems, Universally Unique Identifiers (UUIDs) are better. The uuid-ossp extension helps generate them.

Using `uuid-ossp`

After enabling uuid-ossp, you can create a table with a UUID primary key. Here's how to create a table and insert a row with a generated UUID:

-- First, ensure the extension is enabled (run once in SQL Editor)
-- CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE products (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  name TEXT NOT NULL,
  price NUMERIC
);

INSERT INTO products (name, price) VALUES
('Supabase T-Shirt', 25.00),
('Postgres Mug', 12.50);

SELECT id, name FROM products;

Fuzzy Search with `pg_trgm`

Have you ever searched for something and misspelled it, but still got relevant results? That's fuzzy search!

The pg_trgm extension helps you perform similarity searches based on trigrams (sequences of three characters). It's great for "did you mean?" features or finding close matches.

Using `pg_trgm` for Similarity

Let's enable pg_trgm and see how to find products with names similar to a search term. We'll use the similarity() function.

-- First, ensure the extension is enabled (run once in SQL Editor)
-- CREATE EXTENSION IF NOT EXISTS pg_trgm;

-- Create a sample table if it doesn't exist
CREATE TABLE IF NOT EXISTS articles (
  id SERIAL PRIMARY KEY,
  title TEXT NOT NULL
);

TRUNCATE TABLE articles; -- Clear previous data for fresh run
INSERT INTO articles (title) VALUES
('PostgreSQL Best Practices'),
('Supabase Authentication Guide'),
('Understanding Postgres Extensions'),
('Postgres Performance Tips');

-- Find titles similar to 'Postgres Extension'
SELECT title, similarity(title, 'Postgres Extension') AS score
FROM articles
WHERE similarity(title, 'Postgres Extension') > 0.3
ORDER BY score DESC;

Key-Value with `hstore`

Sometimes, you need to store flexible, unstructured data, like user preferences or product attributes, without creating many new columns.

The hstore extension provides a data type for storing sets of key/value pairs within a single column. It's like a mini-JSON object, but optimized for simple string key-value storage.

Using `hstore`

Let's add an hstore column to a table and store some flexible data. You can then query it using operators like -> to get a value by key.

-- First, ensure the extension is enabled (run once in SQL Editor)
-- CREATE EXTENSION IF NOT EXISTS hstore;

CREATE TABLE IF NOT EXISTS users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  settings HSTORE
);

TRUNCATE TABLE users; -- Clear previous data for fresh run
INSERT INTO users (name, settings) VALUES
('Alice', 'theme=>dark, notifications=>true'),
('Bob', 'theme=>light, language=>en-US');

-- Query for users with dark theme
SELECT name, settings->'theme' AS user_theme
FROM users
WHERE settings->'theme' = 'dark';

Why Use Extensions?

PostgreSQL extensions offer several key benefits:

  • Extended Functionality: Add powerful features not available by default.
  • Performance: Many are highly optimized C functions, offering great speed.
  • Simplicity: Integrate complex logic directly into your database.

However, be mindful of over-reliance and ensure the extension is well-maintained and compatible with your Postgres version.

Extension Check

You've learned about a few powerful PostgreSQL extensions. Let's test your understanding!

Recap: Power Up Your Database!

In this lesson, we explored the world of PostgreSQL extensions! We learned that they are like powerful plugins that enhance your database's capabilities.

  • We saw how to enable extensions in Supabase.
  • We used uuid-ossp for generating unique IDs.
  • We used pg_trgm for fuzzy search.
  • We used hstore for flexible key-value storage.

These extensions help you build more robust and feature-rich applications directly within your database!

Gratis per iniziare

Impara Supabase Backend as a Service con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
11
Lezioni
40

Domande Frequenti

La lezione «Utilizzo delle estensioni di Postgres» è gratuita?

Sì — il testo completo di «Utilizzo delle estensioni di Postgres» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Supabase Backend as a Service, passa a CoddyKit PRO. Il corso Supabase Backend as a Service include 3 lezioni in totale.

Cosa imparerò in «Utilizzo delle estensioni di Postgres»?

Scopra come abilitare e utilizzare potenti estensioni di PostgreSQL per aggiungere funzionalità come la generazione di UUID o la ricerca full-text Eserciti Supabase Backend as a Service con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Supabase Backend as a Service?

Non è richiesta alcuna esperienza precedente. Supabase Backend as a Service su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 3.

Quanto tempo richiede la lezione «Utilizzo delle estensioni di Postgres»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Supabase Backend as a Service?

Sì. Ogni lezione Supabase Backend as a Service include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Utilizzo delle estensioni di Postgres
  2. Dati geospaziali avanzati (PostGIS)
  3. Ricerca full-text e vector embedding con pgvector
← Torna a Supabase Backend as a Service