> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/the-sql-language/functions/functions-uuid.md).

# 9.14. UUID Functions \#

[Table 9.45](#FUNC_UUID_GEN_TABLE) shows the PostgreSQL functions that can be used to generate UUIDs.

**Table 9.45. UUID Generation Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>gen\_random\_uuid</code> ( ) → <code>uuid</code></p><p><code>uuidv4</code> ( ) → <code>uuid</code></p><p>Generates a version 4 (random) UUID</p><p><code>gen\_random\_uuid()</code> → <code>5b30857f-0bfa-48b5-ac0b-5c64e28078d1</code></p><p><code>uuidv4()</code> → <code>b42410ee-132f-42ee-9e4f-09a6485c95b8</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| <p><code>uuidv7</code> ( \[ <em><code>shift</code></em> <code>interval</code> ] ) → <code>uuid</code></p><p>Generates a version 7 (time-ordered) UUID. The timestamp is computed using UNIX timestamp with millisecond precision + sub-millisecond timestamp + random. The optional parameter <em><code>shift</code></em> will shift the computed timestamp by the given <code>interval</code>. Infinite interval values are not accepted. The shifted timestamp must fall within the range supported by UUID version 7's 48-bit millisecond timestamp field: from 1970-01-01 00:00:00 UTC to approximately year 10889. An error is raised if the resulting timestamp is outside this range.</p><p><code>uuidv7()</code> → <code>019535d9-3df7-79fb-b466-fa907fa17f9e</code></p> |

<br>

## Note

The [uuid-ossp](/appendixes/contrib/uuid-ossp.md) module provides additional functions that implement other standard algorithms for generating UUIDs.

[Table 9.46](#FUNC_UUID_EXTRACT_TABLE) shows the PostgreSQL functions that can be used to extract information from UUIDs.

**Table 9.46. UUID Extraction Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>uuid\_extract\_timestamp</code> ( <code>uuid</code> ) → <code>timestamp with time zone</code></p><p>Extracts a <code>timestamp with time zone</code> from a UUID of version 1 or 7. For other versions, this function returns null. Note that the extracted timestamp is not necessarily exactly equal to the time the UUID was generated; this depends on the implementation that generated the UUID.</p><p><code>uuid\_extract\_timestamp('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code> → <code>2025-02-23 21:46:24.503-05</code></p>                                                          |
| <p><code>uuid\_extract\_version</code> ( <code>uuid</code> ) → <code>smallint</code></p><p>Extracts the version from a UUID of one of the variants described by <a href="https://datatracker.ietf.org/doc/html/rfc9562">RFC 9562</a>. For other variants, this function returns null. For example, for a UUID generated by <code>gen\_random\_uuid()</code>, this function will return 4.</p><p><code>uuid\_extract\_version('41db1265-8bc1-4ab3-992f-​885799a4af1d'::uuid)</code> → <code>4</code></p><p><code>uuid\_extract\_version('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code> → <code>7</code></p> |

<br>

PostgreSQL also provides the usual comparison operators shown in [Table 9.1](/the-sql-language/functions/functions-comparison.md#FUNCTIONS-COMPARISON-OP-TABLE) for UUIDs.

See [Section 8.12](/the-sql-language/datatype/datatype-uuid.md) for details on the data type `uuid` in PostgreSQL.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-uuid.html)（英文原文，待翻譯）


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/the-sql-language/functions/functions-uuid.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
