PostgreSQL disables more output

I run the script on my PostgreSQL server:

psql db -f sql.sql 

from bash or in cron script.

It tries to break pages into more or less .

How to disable split on result in psql ?

All I want to do is change the data, I don't care about the exit.

+107
postgresql
Jun 24 2018-12-18T00:
source share
6 answers

To disable pagination, but save the output, use:

 \pset pager off 

To remember this option, add it to ~ / .psqlrc .

See psql manual .

In older versions of Pg, this was just a switch, so \pset pager

To completely disable query output, use \o /dev/null in the psql script.

To suppress psql information output, run it with -q or set QUIET=1 in the environment.




To get the results and throw them away, you can redirect stdout to /dev/null with:

 psql db -f sql.sql >/dev/null 

You can redirect both stdout and stderr with:

 psql db -f sql.sql >&/dev/null 

but I do not recommend this, as it will throw out error information that can warn you that something is wrong. You also get results and discard them, which is inefficient; you better not just produce them first, adjusting your requests.

+182
Jun 25 2018-12-12T00:
source share

I was looking for this too, I found a way in a similar question on ServerFault:

 psql -P pager=off <other params> 

disables paging without suppressing output.

+84
Mar 25 '13 at 17:49
source share

bash, being a shell , has 2 threads , you can redirect this output: stdout and stderr, because this output needs to be redirected somewhere, linux has a specific "drop all" node to / dev / null . Everything that you send there will simply disappear into the void.

(the shells also have an input stream, but I will ignore it here since you asked to suppress the output)

These streams are represented by numbers: 1 for stdout and 2 for stderr.

So, if you only want to redirect stdout, you would do it with the < and > operators (basically where it points to where the data goes)

Suppose we want to suppress stdout (redirecting to / dev / null):

psql db -f sql.sql > /dev/null

As you can see, this is stdout by default, the stream number is not used if you want to use the stream number that you write

psql db -f sql.sql 1> /dev/null

Now, if you want to suppress stderror (thread number 2), you should use

psql db -f sql.sql 2> /dev/null

You can also redirect one thread to another, for example stderror to stdout, which is useful if you want to keep all the outputs somewhere regular and errors.

psql db -f sql.sql 2>&1 > log.txt

remember that between 2>&1

there should be no spaces

Finally, and sometimes the most interesting is the fact that you can suppress all output with &> , because when you want it to be โ€œcompletely silentโ€

psql db -f sql.sql &> /dev/null

+7
Jun 24 '12 at 23:24
source share
 psql db -f sql.sql > /dev/null 
+3
Jun 24 2018-12-12T00:
source share

Here is another option. This has the advantage that you do not need to remember psql parameter names, etc.

 psql ... | cat 
+3
Jun 15 '17 at 17:06 on
source share
 psql -U user -P pager=off -d database -c 'SQL'; 
0
Nov 30 '18 at 13:53
source share



All Articles