Postgres 확장 기능 활용
UUID 생성이나 전문 검색 같은 새로운 기능을 추가할 수 있도록 강력한 PostgreSQL 확장 기능을 활성화하고 사용하는 방법을 알아봅니다.
Postgres 확장 기능 활용은(는) CoddyKit의 무료 Supabase Backend as a Service 강의입니다. 이것은 3개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Supabase Backend as a Service 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Supabase Backend as a Service 강의에는 총 3개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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 UUIDsUnique 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-osspfor generating unique IDs. - We used
pg_trgmfor fuzzy search. - We used
hstorefor flexible key-value storage.
These extensions help you build more robust and feature-rich applications directly within your database!
자주 묻는 질문
“Postgres 확장 기능 활용” 강의는 무료인가요?
네 — “Postgres 확장 기능 활용” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Supabase Backend as a Service 강의 전체를 잠금 해제할 수 있습니다. Supabase Backend as a Service 강의에는 총 3개의 강의가 포함되어 있습니다.
“Postgres 확장 기능 활용”에서 뭘 배우나요?
UUID 생성이나 전문 검색 같은 새로운 기능을 추가할 수 있도록 강력한 PostgreSQL 확장 기능을 활성화하고 사용하는 방법을 알아봅니다. 브라우저에서 직접 실행하는 실습 코드로 Supabase Backend as a Service을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Supabase Backend as a Service을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Supabase Backend as a Service은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 3개 중 1번째 강의입니다.
“Postgres 확장 기능 활용” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Supabase Backend as a Service 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Supabase Backend as a Service 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Postgres 확장 기능 활용
- 고급 지리 공간 데이터(PostGIS)
- pgvector를 활용한 전문 검색과 벡터 임베딩