Vi Nguyen's Dev Notes
  • 📓Dev Notes
    • Buy me a coffee
  • Deployment & Operation
    • Docker Container
      • Add User to Docker Group
      • Clean up unused docker images
      • Backup Container
      • Truncate docker log
    • Git
    • Ansible
      • Ansible - Setup fresh new ubuntu server
      • Run Ansible Playbook on MacOS
    • Linux
      • Zip file with Gzip
      • Linux - add user to sudo group
      • Rsync - copy file on remote server to local
      • Tunnel all docker ports on remote server to localhost
      • Create linux Swapfile
      • Rename a file to folder name
      • Copy and keep permission on linux
    • Database
      • Check if database is reachable with nc
      • PostgreSQL
        • PostgreSQL - Create a new primary key auto-increment column
        • Generate uuid for all records in a table PostgreSQL
        • Backup & Import all database PostgreSQL Inside Docker Container
        • Backup & Import single database PostgreSQL Inside Docker Container
        • PostgreSQL - Create user & grant permission on a database
        • PostgeSQL - Backup database via remote ssh server
      • Mysql
        • Mysql - Create user & grant permission on a database
        • Dump & import Mysql database inside container
        • Mysql - Dump all Mysql database from Mysql
    • Ngnix
      • Nginx - 504 Gateway Timeout error using Nginx as Proxy
      • Download file from server via nginx docker
  • Docker-compose samples
    • docker-compose - n8n
    • docker-compose - PostgreSQL
    • docker-compose caddy as reverse proxy
  • Homelab
    • Rasberry Pi 4B - Bookworm HDMI config
    • Waveshare 3.5" display on Pi 4 -Bookworm 64 Bit
  • Development
    • Development Notes
  • My Resources
    • Blog
    • Geek Tools
    • Dev Note Github Repo
Powered by GitBook
On this page
  1. Deployment & Operation
  2. Database
  3. PostgreSQL

Generate uuid for all records in a table PostgreSQL

  1. Enable the uuid-ossp extension (if not already enabled):

    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
  2. Add a UUID column to your table:

    Let's assume you have a table called my_table and you want to add a UUID column named uuid_column. You can use the following SQL command to add the column:

    ALTER TABLE my_table
    ADD COLUMN uuid_column UUID DEFAULT uuid_generate_v4();

    This command adds a new column to your table with default values generated using the uuid_generate_v4() function.

  3. Update existing rows with UUID values:

    To populate the newly added UUID column with UUIDs for all existing rows, you can use an UPDATE statement:

    UPDATE my_table
    SET uuid_column = uuid_generate_v4();

    This statement will generate a UUID for each row in your table and update the uuid_column with those values.

  4. Optional: Make the UUID column not nullable (if needed):

    By default, the UUID column will allow NULL values. If you want to enforce that the UUID column cannot be NULL, you can use the following SQL command:

    ALTER TABLE my_table
    ALTER COLUMN uuid_column SET NOT NULL;

Last updated 1 year ago