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 8222859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:25:11+00:00 2026-06-07T14:25:11+00:00

Here is the problem: I need to know how to get all PostgreSQL output

  • 0

Here is the problem: I need to know how to get all PostgreSQL output from an executed .sql script to log to a file, including errors and query results.

I have already surrounded my script with \o and \o opening and closing tags, but this only logs the results of queries, which tells me nothing about what did and did not succeed. I have tried piping the results of a call to to PostgreSQL using Unix tools like so:

    $~: psql < filename.sql | tee &> filename.log

… with no success. Filename.log in this case ends up completely empty.

I don’t want to activate universal logging, as I am only concerned with the success/failure of the scripts I have written and keeping a record of this for future reference.

In Oracle, I used SPOOL, in MySQL I used TEE. Both of these worked well enough for my needs, but \o does not have this functionality, and neither does activating logging, as this logs to a single file and I want my logs separated based on which file resulted in those logs.

Any help would be appreciated.

EDIT: the version I am using is 9.1

EDIT: The problem I am having is that using -f and using < to execute .sql files gives me essentially the same results; it doesn’t log the errors, it only logs query results. I need to somehow get stderr (if that’s what is used) to print it’s messages to a file and not just the command line such that the file will essentially look identical to the command line results of running the file, with query results and errors mixed in. The reason I need to do this is because it makes debugging a .sql file much easier and it allows me to keep targeted records while avoiding universal error logging.

  • 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-07T14:25:14+00:00Added an answer on June 7, 2026 at 2:25 pm

    I was using the wrong redirect syntax on the command line. While this is not an ideal solution in my opinion (I have to remember to modify any command I ever run to redirect output to a file), it works, and that’s all I care about.

    To redirect both stdout and stderr to a file to log the results of just one .sql file including errors and queries, one can do the following:

    psql -f scriptname.sql &> logname.txt
    

    Here is the breakdown: -f (as alluded to in the answer from Frank Heikens) is a better command to use than < for reading a .sql script because it shows the line numbers that triggered the errors. &> is a Linux command line trick that puts both stdout and stderr into the file that follows. To limit the log to just errors (just sdterr output), simply use 2> instead of &>. There is no need for a pipe because both streams go to the same place.

    You will notice, however, that the above command takes all of the output from the command and places it into the file, and you therefore do not see any of that output in the command line. This is not ideal for debugging .sql scripts, so the following command can be used instead:

    psql -f scriptname.sql 2>&1 | tee logname.txt
    

    The breakdown here is that the 2>&1 is the long form of &>, but when working with pipes, 2>&1 avoids BASH interpreting the syntax as an error (don’t ask me why). tee simply allows output to go both to the console and to the given file. A filename in all of the above commands can be a full path and can include ~ (for the home directory), and anything else that Linux can interpret as a valid filename and/or path.

    Still, this solution is not ideal as it is external to the .sql scripts that I need to log. However, this method does work. I assume that the history PostgreSQL has with Linux is partially to blame for the dependence on command-line tools to do this sort of thing.

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

Sidebar

Related Questions

Well i need some help here i don't know how to solve this problem.
I have an interesting SQL problem that I need help with. Here is the
I've chosen the title here as my problem is I need to get the
My problem : I need to create draggable widgets (here it's a jslider for
I have a slightly complex problem here. I need to show to the client
Here's the problem, I need to validate the form before submitting in the next
Here is the problem... For school project I need to write parallel application using
I need help with CMD scripts. Here is my problem: I have list of
I need a little push in the right direction. Here's my problem: I have
Ok, so here's the problem I have to solve. I need to write a

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.