In Postgres 9.x, for a column of type UUID, how do I specify a UUID to be generated automatically as a default value for any row insert?

3 Answers 3



Call DEFAULT when defining a column to invoke one of the OSSP uuid functions. The Postgres server will automatically invoke the function every time a row is inserted.

  pkey UUID NOT NULL DEFAULT uuid_generate_v1() , 
  CONSTRAINT pkey_tbl PRIMARY KEY ( pkey )

If you already use the pgcrypto extension, consider the Answer by bpieck.

Plugin Required To Generate UUID

While Postgres out-of-the-box supports storing UUID (Universally Unique Identifier) values in their native 128-bit form, generating UUID values requires a plug-in. In Postgres, a plug-in is known as an extension.

To install an extension, call CREATE EXTENSION. To avoid re-installing, add IF NOT EXISTS. See my blog post for more details, or see this page in StackOverflow.

The extension we want is an open-source library built in C for working with UUIDs, OSSP uuid. A build of this library for Postgres is often bundled with an installation of Postgres such as the graphical installers provided by Enterprise DB or included by cloud providers such as Amazon RDS for PostgreSQL.


Generating Various Kinds Of UUID

See the extension’s doc to see a list of multiple commands offered for generating various kinds of UUID values. To get the original version of UUID built from the computer’s MAC address plus current date-time plus a small random value, call uuid_generate_v1().

SELECT uuid_generate_v1();


Later variations on this theme were developed for alternate kinds of UUIDs. Some people may not want to record the server’s actual MAC address, for example, for security or privacy concerns. The Postgres extension generates five kinds of UUIDs, plus the “nil” UUID 00000000-0000-0000-0000-000000000000.

UUID As Default Value

That method call can be made automatically to generate a default value for any newly inserted row. When defining the column, specify:

DEFAULT uuid_generate_v1()

See that command used in the following example table definition.

CREATE TABLE public.pet_
  species_ text NOT NULL,
  name_ text NOT NULL,
  date_of_birth_ text NOT NULL,
  uuid_ uuid NOT NULL DEFAULT uuid_generate_v1(),  -- <====
  CONSTRAINT pet_pkey_ PRIMARY KEY (uuid_)
ALTER TABLE public.pet_
  OWNER TO postgres;


To query on a UUID column, call PreparedStatement#setObject.

myPreparedStatement.setObject( … , myUuidObject ) ;

See also my Answer on a related Question.


To retrieve the value from a UUID column, call ResultSet#getObject.

UUID uuid = myResultSet.getObject( … , UUID.class ) ;

UUID versions

The uuid-ossp plugin can generate various versions of UUID.

  • uuid_generate_v1()
    Contains MAC address of current computer + current moment. Commonly used, but avoid if you are sensitive about disclosing the MAC of your database server or the time when this value was generated. Defined by specification as a Version 1 UUID.
  • uuid_generate_v1mc()
    Like Version 1, but with a random multicast MAC address instead of the real MAC address. Apparently a way to use Version 1 but substituting another MAC rather than the actual MAC of your database server if you are sensitive about disclosing that fact.
    What is a ‘random multicast MAC’? I do not know exactly. After reading section 4.1.6 of RFC 4122, I suspect this is a random number used in place of the MAC but with bits set to indicate a multicast MAC address rather than the usual unicast so as to distinguish this variation of Version 1 from a usual real-MAC Version 1 UUID.
  • uuid_generate_v3( namespace uuid, name text )
    Contains an MD5 hash of text you provide. Defined by specification as a Version 3 UUID, namespace-based UUID.
  • uuid_generate_v4()
    Based on randomly-generated data for 121-122 of the 128 bits. Six or seven bits used to indicate Version & Variant. This kind of UUID is practical only if implemented with a cryptographically-strong random generator. Defined by specification as a Version 4 UUID.
  • uuid_generate_v5( namespace uuid, name text )
    Same as Version 3 but using SHA1 hashing. Defined by specification as Version 5 UUID.
  • uuid_nil()
    A special case, all bits set to zero 00000000-0000-0000-0000-000000000000. Used as a flag for an unknown UUID value. Known as a nil UUID.

To compare types, see Question, Which UUID version to use?

If you are interested only in Version 4 (randomly generated), and are already using pgcrypto, see the Answer by bpieck.

If you are curious about Versions 3 & 5, see this Question, Generating v5 UUID. What is name and namespace?.

For more discussion, see my Answer to a similar Question and my blog post UUID values from JDBC to Postgres.


pgcrypto extension

Just a small addition to Basil’s very detailed answer.

Since currently most are using pgcrypto, instead of uuid_generate_v4() you can use gen_random_uuid() for a Version 4 UUID value.

First, enable pgcrypto in your Postgres.


Just set DEFAULT of a column to

DEFAULT gen_random_uuid()
  • 5
    Generated 100mil uuids using both methods, they come in at pretty much the same ~9sec. (Tested under Postgres 14 - which doesnt require the pgcrypto extension anymore for this function)
    – BARNZ
    Commented Oct 11, 2021 at 5:55

If using Postgres 13 or newer you do not need any modifications or extra plugins and can just use gen_random_uuid()

just tested on default AWS Aurora Serverless v2 with Postgres v14

p.s. you can see my default id is actually with prefix wor- while it's workspace id. I add prefixes to all tables so when i get ID from anywhere - logs, support etc. i can tell exactly what to search for.

concat('wor-', gen_random_uuid())

enter image description here

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.