Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:49:56+00:00 2026-06-07T04:49:56+00:00

In other statistical programs, it’s possible to create a log file that shows the

  • 0

In other statistical programs, it’s possible to create a log file that shows the output issued as a result of a command. Is it possible to do something similar in SQL?

In particular, I’d like to have a single .sql file with many queries and to then output each result to a text file.

I’m using PostgreSQL and Navicat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:49:58+00:00Added an answer on June 7, 2026 at 4:49 am

    plpgsql function and COPY

    One way would be to put the SQL script into a plpgsql function, where you can write the individual return values to files with COPY and compile a report from intermediary results just like you need it.

    This has additional effect that may or may not be desirable. Like, you can grant or revoke permission to the whole function to arbitrary roles. Read about SECURITY DEFINER in the manual. And the syntax will be verified when you save the function – however, only superficially (there are plans to change that in the future). More details in this answer on dba.SE.

    Basic example:

    CREATE OR REPLACE FUNCTION func()
      RETURNS void AS
    $BODY$
    BEGIN
    
    COPY (SELECT * FROM tbl WHERE foo) TO '/path/to/my/file/tbl.csv';
    
    COPY (SELECT * FROM tbl2 WHERE NOT bar) TO '/path/to/my/file/tbl2.csv';
    
    END;
    $BODY$
      LANGUAGE plpgsql;
    

    Of course, you need to have the necessary privileges in the database and in the file system.
    Call it from the shell:

    psql mydb -c 'SELECT func();'
    

    psql switching between meta commands and SQL

    #!/bin/sh
    
    BASEDIR='/var/lib/postgresql/outfiles/'
    
    echo "
    \\o $OUTDIR/file1.txt  \\\\\\\\ SELECT * FROM tbl1;
    \\o $OUTDIR/file2.txt  \\\\\\\\ SELECT * FROM tbl2;
    \\o $OUTDIR/file3.txt  \\\\\\\\ SELECT * FROM tbl3;" | psql event -p 5432 -t -A
    

    That’s right, 8 backslashes. Results from a double backslash that gets interpreted two times, so you have to double them two times.

    I quote the manual about the meta-commands \o:

    Saves future query results to the file filename or …

    and \\:

    command must be either a command string that is completely parsable by
    the server (i.e., it contains no psql-specific features), or a single
    backslash command. Thus you cannot mix SQL and psql meta-commands with
    this option. To achieve that, you could pipe the string into psql,
    like this: echo ‘\x \\ SELECT * FROM foo;’ | psql. (\\ is the
    separator meta-command.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Other than using the Column chart and naming appropriately is it possible to create
Other than reading the GetFileName() file and reading to line GetFileLineNumber() of a stackframe
Other than that I don't know if I can reproduce it now that it's
Other than the fact that PNG is a more common image format, is there
I have written some code that provides statistical information about visitors to a website
This question comes from a range of other questions that all deal with essentially
I have a table for a Statistical project. Structure is like this: CREATE TABLE
I have an XML document collection, an inverted file indexer, and a command-line tool
With RSS feeds how do you set up google analytics (or any other statistical
Other questions dealing with use of match_parent pre 2.2 have answers stating one should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.