In PostgreSQL, generating universally unique identifiers (UUIDs) automatically for primary keys or any other fields in your tables can greatly enhance the robustness and scalability of your database design. UUIDs provide a more secure and collision-resistant method of uniquely identifying records compared to traditional serial numbers. This article will guide you through the process of auto-generating UUIDs in PostgreSQL with a practical example.
Why Use UUIDs?
- Global Uniqueness: UUIDs ensure that every identifier is unique across all databases, not just within a single table.
- Security: UUIDs are harder to guess, making them more secure than sequential identifiers.
- Scalability: They allow distributed systems to generate unique identifiers independently.
Creating a Table with Auto-Generated UUIDs
Now, let’s create a table named transactions with a UUID primary key that is automatically generated using the gen_random_uuid() function provided by the pgcrypto extension.
CREATE TABLE transactions (
transactions_id uuid primary key default gen_random_uuid() not null,
fk_customer_id UUID,
amount double precision,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Explanation
transaction_id uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL: This line definestransaction_idas a UUID field that serves as the primary key. TheDEFAULT gen_random_uuid()clause specifies that a new UUID will be automatically generated for each new row inserted into the table. TheNOT NULLconstraint ensures that this field cannot be null.fk_customer_id UUID: This field can be used to store the UUID of a related customer, assuming you have a customers table with UUIDs.amount DOUBLE PRECISION: This field stores the transaction amount.created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP: This field records the timestamp when the transaction is created, defaulting to the current time.
Inserting Data into the Table
When you insert data into the transactions table, you do not need to specify a value for the transaction_id field. PostgreSQL will automatically generate a UUID for you.
INSERT INTO transactions ( fk_customer_id, amount) VALUES
( 'e9d8b7d9-8176-4c5b-bcb5-a35e7a1e6d32', 100.43),
( 'e9d8b7d9-8176-4c5b-bcb5-a35e7a1e6d32', 70.4),
( 'e9d8b7d9-8176-4c5b-bcb5-a35e7a1e6d32', 89.11);
Verifying the Data
To verify that UUIDs are being generated correctly, you can query the transactions table:
select * from transactions where fk_customer_id = 'e9d8b7d9-8176-4c5b-bcb5-a35e7a1e6d32';
This query will return all the records in the transactions table, including the auto-generated UUIDs.

Conclusion
Using UUIDs in PostgreSQL is a powerful way to ensure that each record is uniquely and securely identified across distributed systems. This approach enhances both the security and scalability of your database applications.
If you found this article helpful, please give it a like and leave a comment below! Your feedback and engagement help me create more content that benefits the community. Feel free to share your thoughts and any questions you might have. Happy coding!







