Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

236
Vistas
How can I create a Serial ID with as a string with common prefix (ie tag_1, tag_2, tag_3) in Postgres

In Postrges, I know how to create a table with a column that has a serial (number) id:

CREATE TABLE Tag (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);

I prefer to have my ids have a human readable aspect to them though. I usually do something like tag_1, tag_2, tag_3 usr_1, usr_2, usr_3.

I've seen instructions on how to generate uuids (Generating a UUID in Postgres for Insert statement?), but I don't really need that. The basic serial number is more than enough, I just want to prefix tag_ to the beginning.

How can I create a Serial ID with as a string with common prefix?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do that with a sequence, which you can add to your table as a default value concatenated via || with the prefix you want e.g.

CREATE SEQUENCE tag_seq START WITH 1;

CREATE TABLE tag 
    (id TEXT NOT NULL DEFAULT 'tag_'||nextval('tag_seq'::regclass), 
    name TEXT);
    
INSERT INTO tag (name) VALUES ('foo'),('bar');    
SELECT * FROM tag;
  id   | name 
-------+------
 tag_1 | foo
 tag_2 | bar
(2 Zeilen)

Demo: db<>fiddle

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda