-2

I am using below command to get result for my SQL query.

su - postgres -c 'psql -d dbname' with stdin "COPY ( my SQL query ) TO STDOUT WITH CSV HEADER"

This works fine on my server but on different machine it is printing bash warning with output of SQL query.

For example -

/etc/profile: line 46: HISTSIZE: readonly variable
/etc/profile: line 50: HISTCONTROL: readonly variable
/etc/profile.d/20-tmout.sh: line 1: TMOUT: readonly variable
/etc/profile.d/history.sh: line 6: hcmnt_tty: readonly variable
name
abc

Please let me know anyway so that I can skip above warning messages and only get data.

If I would like to use /dev/null in this case how to modify above command to get data only.

kd12
  • 1,291
  • 1
  • 13
  • 22

3 Answers3

0

if what you mean is "how to discard only error output?", the way to go is to redirect the standard error stream to oblivion (/dev/null), like so:

your-command 2>/dev/null

that way, if the command outputs data to standard out, it passes through, but any output to the standard error socket is discarded, so you won't see these error messages.

by the way, 2 here is a shorthand file descriptor for the standard error.

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
  • I don't think sweeping data under the rug is the desired effect here – Calvin Taylor Nov 08 '17 at 10:54
  • @EliranMalka - I mentioned 2>/dev/null after my command it won't show stderr related to SQL command using psql. or is there anyway I can skip warnings from su - postgres related to /etc/profile. – kd12 Nov 08 '17 at 11:12
  • i don't know such way, maybe some psql/postgres expert will share her knowledge.. – Eliran Malka Nov 08 '17 at 11:38
0

Sorry this is untested, but I hit this same error, your db session isn't read/write. You can echo the statements to psql to force a proper session as follows. I'm unsure as to how stdin may be effected

echo 'SET TRANSACTION READ WRITE; SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE ; COPY ( my SQL query ) TO STDOUT WITH CSV HEADER' | su - postgres -c 'psql -d dbname' with stdin 
Calvin Taylor
  • 664
  • 4
  • 15
0

caution - bash hack

su - postgres -c 'psql -d dbname' with stdin "COPY ( my SQL query ) TO STDOUT WITH CSV HEADER" | grep -v "readonly"
Calvin Taylor
  • 664
  • 4
  • 15