Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda