Work with UUID type in PostgreSQL- An easy way

We can easily work with UUID data type in PostgreSQL. Here, I am describing the way how we make it.


Extension “uuid-ossp” must be installed.

Check the extension: SELECT * FROM pg_available_extensions;

If not available then run following query to add this: CREATE EXTENSION "uuid-ossp";

Verify using: SELECT * FROM pg_extension;

Create a sample table with UUID type primary key-

id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
age integer

Now insert into table “People”. Primary key id will be automatically inserted with UUID format value.
INSERT INTO People(age) VALUES (10);

Fetch data from “People” table-

Select * from People;

If you face following error “error: Relation ‘tableName’ does not exist” while you are working with SQL query (but table already in database) then to fix this┬ájust write table name in quotation mark i.e. “Book”.

Take a look on following links-