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

209
Visualizações
Pipe multiple SQL commands to psql CLI

I have a set of linux applications that are being composed together via pipes

some_application | awk '{print "INSERT INTO my_table VALUES (\x27" $2 "\x27," $3 ")"'

The output of which will be a series of SQL INSERT commands:

INSERT INTO my_table VALUES ('foo',42)
INSERT INTO my_table VALUES ('bar',43)

How can these multiple commands be piped into psql?

There is a similar question which answers how to execute a single command using the -c parameter for psql. But that answer is not ideal for my scenario because it requires establishing a new connection for each command; I would prefer to setup a single connection and then pipe all commands through that one session.

Thank you in advance for your consideration and response.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can pipe it into psql directly, but you need semicolon terminators for each line.

some_application | awk '{print "INSERT INTO my_table (\'" $2 "\'," $3 ");"' | psql <your connection switches>

You can test your connection parms by doing something innocuous like so:

echo "select count(*) from pg_tables;" | psql <your parms>

You will see the result of the query if the connection information is correct.

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