> For the complete documentation index, see [llms.txt](https://onemorebottlees-organization.gitbook.io/onemorebottlees-til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://onemorebottlees-organization.gitbook.io/onemorebottlees-til/lecture/nomad-coders/sql-master-class/18-postgresql-extensions.md).

# #18 PostgreSQL Extensions

## #18.0 Introduction

Extention을 활용해 기존 database가 하지 못하던 것들을 가능하게 해줌 ⇒ postgreSQL 의 가장 큰 장점

```sql
select * from pg_available_extensions;

-- 활성화 / 비활성화
CREATE EXTENSION hstore;
DROP EXTENSION hstore;

-- 사용한 extension 확인
SELECT * FROM pg_extension;
```

## #18.1 HSTORE

```sql
CREATE TABLE users (
  user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  prefs HSTORE
);

INSERT INTO users (prefs) VALUES
('theme => dark, lang => kr, notifications => off'),
('theme => light, lang => es, notifications => on, push_notifications => on, email_notifications => off'),
('theme => dark, lang => it, start_page => dashboard, font_size => large');

SELECT * FROM USERS;
-- HSSTORE, JSON 타입을 사용하면 중첩된 정보를 가질 수 있는데 HSTORE는 이를 방지함

SELECT
  user_id,
  prefs -> 'theme',
  prefs -> ARRAY['lang', 'notifications'],
  prefs ? 'font_size' AS has_font_size,
  prefs ?| ARRAY['push_notifications', 'start_page']
FROM users;

SELECT
  user_id,
  akeys(prefs),
  avals(prefs)
FROM users;

SELECT
  user_id,
  each(prefs)
FROM users;

-- UPDATE
UPDATE users
SET 
  prefs['theme'] = 'light'
WHERE user_id = 1;

UPDATE users
SET
  prefs = prefs || hstore(
  ARRAY['currency', 'cookies_ok'],
  ARRAY['krw', 'yes']
)
  
-- DELETE
UPDATE users
SET prefs = delete(prefs, 'cookies_ok');

SELECT * from users;

DROP TABLE users
```

## #18.2 PGCrypto

```sql
CREATE TABLE users (
  user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  username VARCHAR(100),
  password VARCHAR(100)
)

-- password 해싱
INSERT INTO users (username, password)
VALUES ('son', crypt('user_password', gen_salt('bf')));

SELECT username
FROM users
WHERE 
  username = 'son' AND
  password = crypt('user_password', password);
```

## #18.3 UUID

```sql
CREATE EXTENSION "uuid-ossp";

-- BIGINT의 한계 보완 가능
CREATE TABLE users (
  user_id UUID PRIMARY KEY DEFAULT(uuid_generate_v4()),
  username VARCHAR(100),
  password VARCHAR(100)
)

INSERT INTO users (username, password) VALUES ('SON', '1234');

SELECT * FROM users;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://onemorebottlees-organization.gitbook.io/onemorebottlees-til/lecture/nomad-coders/sql-master-class/18-postgresql-extensions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
