How capture "ANALYZE VERBOSE tablename" output into logfile - postgresql

I would like to capture output of "ANALYZE VERBOSE TableName" into output file when executed from shell script.
Want to capture this:
INFO: analyzing "tablename" INFO: "tablename": scanned 1 of 1 pages,
containing 7 live rows and 2 dead rows; 7 rows in sample, 7 estimated
total rows
Using this command
psql -h $DB_HOST_NAME -U $DB_USER $DB_NAME -f query.txt --echo-errors --echo-queries >> output.log
But its only capturing text "ANALYZE" not capturing entire text.
Please suggest how to print entire text into output file.

The output you are trying to capture is directed through stderr, so you need to capture/redirect that output at the shell level. This can be tempermental based on your versions of your OS / Shell. On OSX with 3.2.57, you would need to use:
psql -h $DB_HOST_NAME -U $DB_USER $DB_NAME -f query.txt --echo-queries >> output.log 2>&1 ;
If that doesn't work, try looking up the specifics for whatever combination of OS/shell you are using.

Related

a powershell script to read data from a postgres table

I am creating a powershell script to read data from a postgres DB
But any lines given after the psql.exe command does not works
after the psql.exe line the console asks for the password
and does nothing it's only when I press Ctrl+C the other lines get executed
I tried using Start-Job but then I am unable to read the output of my select command it only returns the following line
Job started: System.Management.Automation.PSRemotingJob stating that the job has started
I also tried the Invoke-Command but that too didn't help.
Can anyone help me with a simple sample that explains how to enter password for the psql.exe cmd and how to read the output from the select cmd
I am sharing the approach that worked for me
$env:PGPASSWORD='password'
$result=Write-Output "Select * FROM public.table_name" | & $psql -h 127.0.0.1 -p 5432 -U -U postgres -d database_name
Now you can access the output of the select from the result variable.
You can use a for method and iterate over result to read each row.

How do I prevent PSQL from outputting the number of rows?

Currently I can almost get a CSV from psql by simply running
psql -A -t -F'\t' -c "SELECT ...;" > myfile.csv
However it returns the number of rows at the end of the file. I can fix his with head -n -1
psql -A -t -F'\t' | head -n -1 | -c "SELECT ...;"
But with very large files seems like overkill. Is here a flag in psql where I can turn off number of records returned?
There is a number of common ways to get a CSV from PostrgeSQL (see e.g. this question). However not all of these ways are appropriate when working with Redshift, partly because Amazon Redshift is based on Postgres 8.0.2.
One can try to use --pset="footer=off" option to prevent psql from outputting the number of rows. Also, please consult 8.0.26 psql documentation.

psql - write a query and the query's output to a file

In postgresql 9.3.1, when interactively developing a query using the psql command, the end result is sometimes to write the query results to a file:
boron.production=> \o /tmp/output
boron.production=> select 1;
boron.production=> \o
boron.production=> \q
$ cat /tmp/output
?column?
----------
1
(1 row)
This works fine. But how can I get the query itself to be written to the file along with the query results?
I've tried giving psql the --echo-queries switch:
-e, --echo-queries
Copy all SQL commands sent to the server to standard output as well.
This is equivalent to setting the variable ECHO to queries.
But this always echoes to stdout, not to the file I gave with the \o command.
I've tried the --echo-all switch as well, but it does not appear to echo interactive input.
Using command editing, I can repeat the query with \qecho in front of it. That works, but is tedious.
Is there any way to direct an interactive psql session to write both the query and the query output to a file?
You can try redirecting the stdout to a file directly from your shell (Win or Linux should work)
psql -U postgres -c "select 1 as result" -e nomedb >> hello.txt
This has the drawback of not letting you see the output interactively. If that's a problem, you can either tail the output file in a separate terminal, or, if in *nix, use the tee utility:
psql -U postgres -c "select 1 as result" -e nomedb | tee hello.txt
Hope this helps!
Luca
I know this is an old question, but at least in 9.3 and current versions this is possible using Query Buffer meta-commands shown in the documentation or \? from the psql console: https://www.postgresql.org/docs/9.3/static/app-psql.html
\w or \write filename
\w or \write |command
Outputs the current query buffer to the file filename or pipes it to the shell command command.
Please try this format as I got the output from the same:
psql -h $host -p $port -q -U $user -d $Dbname -c "SELECT \"Employee-id\",\"Employee-name\" FROM Employee_table" >> Employee_Date.csv
I need the output in a CSV file.

Making an empty output from psql

I am running a psql command that executes a complex query. There's nothing that query produces, as such, psql returns "(No rows)" in the output.
Is there a way to make psql to return an empty string?
I've tried using --pset=tuples-only=on and --pset=footer=off and -q in all variations, and it doesn't seem to work.
Footer option works while in psql shell prompt, but doesn't work from script.
Tried on 9.1.7, need this for 8.4, 9.1 and 9.2.
May be good enough:
$ psql -Axt -c 'select 1 where 1=0'
produces an empty string
EDIT following comments:
The command above produces an empty line, so that includes and end-of-line.
To produce nothing at all, remove the -x option.
Not that it would make any difference to the shell anyway, as shown below:
with -x:
r=`psql -Atx -d test -c "select 1 where 1=0"`
echo $r | od -c
0000000 \n
0000001
without -x:
r=`psql -At -d test -c "select 1 where 1=0"`
echo $r | od -c
0000000 \n
0000001
Is there a way to make psql to return an empty string?
You can simply append SELECT '' after the first query.
Ex.:
psql -Atp5432 mydb -c "SELECT 1 WHERE FALSE; SELECT ''"
Replace SELECT 1 WHERE FALSE with your complex query that doesn't return a row.

psql -o not what I expected (how to output db response to an output file)

I am creating a PostgreSQL database from the command line (i.e. using psql).
There are some errors in my SQL statements and I want to find out where the errors are occuring (too many objects to fill the screen buffer - so I need to save thios to file)
I have tried just about everything, from using the -o option, the -L option and using tee - I still cant capture the information that scrolls past on the screen.
How do I log this?
This is what I have tried so far:
psql -U -o dbcreate.log -f file.sql
psql -U -L dbcreate.log -f file.sql
psql -U -a -f file.sql | tee dbcreate.log
NONE of which results in the data flashing accross the screen being logged to file - how do I do this?
You need to redirect stderr. On Un*x and Linux:
psql ... 2>error.log
or both stdout and stderr:
psql ... &>error.log
On the other hand if you like to investigate the errors one by one:
psql -v ON_ERROR_STOP=1 ...
A helpful article about executing SQL scripts with psql - here.