3

The command

psql -d template_postgis2 -f /usr/share/postgresql/9.1/contrib/postgis-2.1/postgis.sql

returns a list of errors that is too long to be displayed by scrolling up to the beginning of the error.

I tried the same command with "| more" and "| less" added up at the end of the command but it does not display the message page by page as it should.

I also tried to put the output in a file with "> file.txt" but I do not get in this new file what is displayed on the screen!!

I don't understand why. I guess i can't do this with a psql command?? Is there any other way to get all the error message?

Thanks for your help!

1
  • Did you try terminal pager program (pg)?
    – devav2
    Sep 17, 2012 at 18:40

3 Answers 3

4

The errors which most of the programs print are sent to stderr (Standard Error) stream, whereas the output of the program is sent to stdout (Standard Output) stream.

Each stream has a file descriptor, which you can think of as a number which identifies a stream. stderr has file descriptor 2, and the value for stdout is 1.

  • When you run
    command > file.txt

          this is equivalent to running,

    command 1> file.txt

          which has a meaning - redirect the contents of stdout to the file file.txt.

  • However, if you want to redirect the contents of stderr only, you'd write:
    command 2> file.txt
  • And to redirect both stdout and stderr, you'd either write:
    command &> file.txt

                 OR

    command > file.txt 2>&1

            You can interpret the second command as - Send the output of command to file.txt, and also merge stderr with stdout.

  • Similarly for sending errors to less or more, first merge stderr to stdout and then send it to the correspoding program.
    command 2>&1 | less
0
2

If you are trying to trap STDERR output to more/less or direct to a file, you need to tell it to do so. Simply using | or > will only grab STDOUT, and not STDERR. To grab both, you need to do something like:

psql -d ... 2>1 | less

This will redirect STDERR into STDOUT, and then both should appear in less when you are trying to page through the output.

0

I think dobey and green7 have the better answers for your particular problem, but, for completeness:

I may get an error or message I didn't expect and not want to run the command again, or don't want to take the time. Therefore whenever I install a new operating system I start the gnome-terminal, go to Edit->Preferences->Scrolling Tab, and click the unlimited scrolling checkbox.

When using a kernel virtual console you can scroll back with Shift-PageUp.

When using less|more, or not using either, I sometimes find it easier to understand messages if I widen the terminal screen to be big enough horizontally to hold most of the message to eliminate line folding.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .