> 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/appendixes/contrib/uuid-ossp.md).

# F.49. uuid-ossp — a UUID generator \#

[F.49.1. `uuid-ossp` Functions](#UUID-OSSP-FUNCTIONS-SECT)

[F.49.2. Building `uuid-ossp`](#UUID-OSSP-BUILDING)

[F.49.3. Author](#UUID-OSSP-AUTHOR)

The `uuid-ossp` module provides functions to generate universally unique identifiers (UUIDs) using one of several standard algorithms. There are also functions to produce certain special UUID constants. This module is only necessary for special requirements beyond what is available in core PostgreSQL. See [Section 9.14](/the-sql-language/functions/functions-uuid.md) for built-in ways to generate UUIDs.

This module is considered “trusted”, that is, it can be installed by non-superusers who have `CREATE` privilege on the current database.

## F.49.1. `uuid-ossp` Functions [#](#UUID-OSSP-FUNCTIONS-SECT)

[Table F.35](#UUID-OSSP-FUNCTIONS) shows the functions available to generate UUIDs. The relevant standards ITU-T Rec. X.667, ISO/IEC 9834-8:2005, and [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) specify four algorithms for generating UUIDs, identified by the version numbers 1, 3, 4, and 5. (There is no version 2 algorithm.) Each of these algorithms could be suitable for a different set of applications.

**Table F.35. Functions for UUID Generation**

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>Function</p><p>Description</p></td></tr><tr><td><p><code>uuid_generate_v1</code> () → <code>uuid</code></p><p>Generates a version 1 UUID. This involves the MAC address of the computer and a time stamp. Note that UUIDs of this kind reveal the identity of the computer that created the identifier and the time at which it did so, which might make it unsuitable for certain security-sensitive applications.</p></td></tr><tr><td><p><code>uuid_generate_v1mc</code> () → <code>uuid</code></p><p>Generates a version 1 UUID, but uses a random multicast MAC address instead of the real MAC address of the computer.</p></td></tr><tr><td><p><code>uuid_generate_v3</code> ( <em><code>namespace</code></em> <code>uuid</code>, <em><code>name</code></em> <code>text</code> ) → <code>uuid</code></p><p>Generates a version 3 UUID in the given namespace using the specified input name. The namespace should be one of the special constants produced by the <code>uuid_ns_*()</code> functions shown in <a href="#UUID-OSSP-CONSTANTS">Table F.36</a>. (It could be any UUID in theory.) The name is an identifier in the selected namespace.</p><p>For example:</p><pre><code>SELECT uuid_generate_v3(uuid_ns_url(), 'http://www.postgresql.org');
</code></pre><pre><code>    The name parameter will be MD5-hashed, so the cleartext cannot be
    derived from the generated UUID.
    The generation of UUIDs by this method has no random or
    environment-dependent element and is therefore reproducible.
   &#x3C;/p>&#x3C;/td>&#x3C;/tr>&#x3C;tr>&#x3C;td class="func_table_entry">&#x3C;p class="func_signature">
</code></pre><p><code>uuid_generate_v4</code> () → <code>uuid</code></p><p>Generates a version 4 UUID, which is derived entirely from random numbers.</p></td></tr><tr><td><p><code>uuid_generate_v5</code> ( <em><code>namespace</code></em> <code>uuid</code>, <em><code>name</code></em> <code>text</code> ) → <code>uuid</code></p><p>Generates a version 5 UUID, which works like a version 3 UUID except that SHA-1 is used as a hashing method. Version 5 should be preferred over version 3 because SHA-1 is thought to be more secure than MD5.</p></td></tr></tbody></table>

<br>

**Table F.36. Functions Returning UUID Constants**

| <p>Function</p><p>Description</p>                                                                                                                                                                                                     |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>uuid\_nil</code> () → <code>uuid</code></p><p>Returns a “nil” UUID constant, which does not occur as a real UUID.</p>                                                                                                        |
| <p><code>uuid\_ns\_dns</code> () → <code>uuid</code></p><p>Returns a constant designating the DNS namespace for UUIDs.</p>                                                                                                            |
| <p><code>uuid\_ns\_url</code> () → <code>uuid</code></p><p>Returns a constant designating the URL namespace for UUIDs.</p>                                                                                                            |
| <p><code>uuid\_ns\_oid</code> () → <code>uuid</code></p><p>Returns a constant designating the ISO object identifier (OID) namespace for UUIDs. (This pertains to ASN.1 OIDs, which are unrelated to the OIDs used in PostgreSQL.)</p> |
| <p><code>uuid\_ns\_x500</code> () → <code>uuid</code></p><p>Returns a constant designating the X.500 distinguished name (DN) namespace for UUIDs.</p>                                                                                 |

<br>

## F.49.2. Building `uuid-ossp` [#](#UUID-OSSP-BUILDING)

Historically this module depended on the OSSP UUID library, which accounts for the module's name. While the OSSP UUID library can still be found at <http://www.ossp.org/pkg/lib/uuid/>, it is not well maintained, and is becoming increasingly difficult to port to newer platforms. `uuid-ossp` can now be built without the OSSP library on some platforms. On FreeBSD and some other BSD-derived platforms, suitable UUID creation functions are included in the core `libc` library. On Linux, macOS, and some other platforms, suitable functions are provided in the `libuuid` library, which originally came from the `e2fsprogs` project (though on modern Linux it is considered part of `util-linux-ng`). When invoking `configure`, specify `--with-uuid=bsd` to use the BSD functions, or `--with-uuid=e2fs` to use `e2fsprogs`' `libuuid`, or `--with-uuid=ossp` to use the OSSP UUID library. More than one of these libraries might be available on a particular machine, so `configure` does not automatically choose one.

## F.49.3. Author [#](#UUID-OSSP-AUTHOR)

Peter Eisentraut `<peter_e@gmx.net>`

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/uuid-ossp.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/appendixes/contrib/uuid-ossp.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.
