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

491
Vistas
Postgresql: run SQL query script and export to csv

I am running Postgresql version 9.3 on Ubuntu. I am wondering if there is a way to run a select statement stored in a script on a Postgres database and then send the results to csv. I know how to do either of the two by themselves; for example, exporting to csv (with the query explicitly written out):

COPY(select * from mytable) to 'path/to/file.csv' with csv header;

and running an sql script:

\i 'myscript.sql'

But after searching around I could not figure out how to do both in one action. That is, I could not figure out how to export to a csv when running a query script rather than typing out a select statement manually.

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

0

\set query `cat /path/to/myscript.sql`
copy (:query) to '/path/to/file.csv' with csv header;

SQL Interpolation in psql

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