db2 "export to filename.csv" giving IO error - database

I am trying to export data using JDBC connection (I am using DB2 database) but it is failing and giving following error:
Caused by: com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -3001, SQLSTATE: , SQLERRMC: sqlofopn -2029060079
Query I used:
call admin_cmd('EXPORT TO /home/user/test_1/db_extract.csv OF DEL
MODIFIED BY NOCHARDEL SELECT * from mytable fetch first 5 rows only');
I Gave 755 access to test_1 folder as well.
I tried removing the admin_cmd as well but getting BEGIN OF STATEMENT error
And also tried the same query using putty, but no luck I am getting this error :
SQL3001C An I/O error (reason = "sqlofopn -2029060079") occurred
while opening the output file.

You need to either grant permission to the db2 fenced user and/or the group that the db2 fenced user is a member of. For example:
-- file /tmp/stack.sql
connect to pocdb user proksch using in4mix;
call admin_cmd('export to /stack/my.unl of del select * from proksch.foo');
connect reset;
terminate;
The following stack.sh script was run as root (or another user than can set acls dynamically)
#!/bin/bash
# ran as root to set acts
DB2=/home/db2inst1/sqllib/bin/db2
function rmperms {
rm -f /stack/my.unl > /dev/null 2> /dev/null
setfacl -x user:db2inst1 /stack
setfacl -x user:db2fence /stack
setfacl -x group:db2 /stack
}
function setperms {
setfacl -m $1 /stack
}
function getperms {
echo " "
echo "Perms on /stack"
ls -l / | grep stack
getfacl /stack --tabular --absolute-names --recursive
echo " "
}
rmperms
getperms
su --command="${DB2} -tvf /tmp/stack.sql" db2inst1
rmperms
setperms user:db2inst1:rwx
getperms
su --command="${DB2} -tvf /tmp/stack.sql" db2inst1
rmperms
setperms user:db2fence:rwx
getperms
su --command="${DB2} -tvf /tmp/stack.sql" db2inst1
rmperms
setperms group:db2:rwx
getperms
su --command="${DB2} -tvf /tmp/stack.sql" db2inst1
Yields the following results:
Perms on /stack
drwxr-xr-x+ 2 unload ops 4096 Mar 22 16:17 stack
# file: /stack
USER unload rwx
GROUP ops r-x
mask r-x
other r-x
connect to pocdb user proksch using
Database Connection Information
Database server = DB2/LINUXX8664 10.5.3
SQL authorization ID = PROKSCH
Local database alias = POCDB
call admin_cmd('export to /stack/my.unl of del select * from proksch.foo')
SQL3001C An I/O error (reason = "sqlofopn -2079391743") occurred while
opening the output file.
connect reset
DB20000I The SQL command completed successfully.
terminate
DB20000I The TERMINATE command completed successfully.
Perms on /stack
drwxrwxr-x+ 2 unload ops 4096 Mar 22 16:17 stack
# file: /stack
USER unload rwx
user db2inst1 rwx
GROUP ops r-x
mask rwx
other r-x
connect to pocdb user proksch using
Database Connection Information
Database server = DB2/LINUXX8664 10.5.3
SQL authorization ID = PROKSCH
Local database alias = POCDB
call admin_cmd('export to /stack/my.unl of del select * from proksch.foo')
SQL3001C An I/O error (reason = "sqlofopn -2079391743") occurred while
opening the output file.
connect reset
DB20000I The SQL command completed successfully.
terminate
DB20000I The TERMINATE command completed successfully.
Perms on /stack
drwxrwxr-x+ 2 unload ops 4096 Mar 22 16:17 stack
# file: /stack
USER unload rwx
user db2fence rwx
GROUP ops r-x
mask rwx
other r-x
connect to pocdb user proksch using
Database Connection Information
Database server = DB2/LINUXX8664 10.5.3
SQL authorization ID = PROKSCH
Local database alias = POCDB
call admin_cmd('export to /stack/my.unl of del select * from proksch.foo')
Result set 1
--------------
ROWS_EXPORTED MSG_RETRIEVAL MSG_REMOVAL
-------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5 - -
1 record(s) selected.
Return Status = 0
connect reset
DB20000I The SQL command completed successfully.
terminate
DB20000I The TERMINATE command completed successfully.
Perms on /stack
drwxrwxr-x+ 2 unload ops 4096 Mar 22 16:17 stack
# file: /stack
USER unload rwx
GROUP ops r-x
group db2 rwx
mask rwx
other r-x
connect to pocdb user proksch using
Database Connection Information
Database server = DB2/LINUXX8664 10.5.3
SQL authorization ID = PROKSCH
Local database alias = POCDB
call admin_cmd('export to /stack/my.unl of del select * from proksch.foo')
Result set 1
--------------
ROWS_EXPORTED MSG_RETRIEVAL MSG_REMOVAL
-------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5 - -
1 record(s) selected.
Return Status = 0
connect reset
DB20000I The SQL command completed successfully.
terminate
DB20000I The TERMINATE command completed successfully.

In windows, the problem was in path access.
Running db2 with admin privileges solved the problem (the problem can be also solved by change paths to files).

Related

Db2 relocate from v10 to v11 on LUW with upgrade

here is my problem:
I have a server with DB2 v10.5.9 and v11.1.4.4 installed. I updated the server, instances and dbs that were there at the time and it all wirks great. Now I have added a new volume to the system that contains a DB in version 10.5.9 and I need to relocate it and upgrade it to v11.1.4.4. The relocate within v10 is not a problem but I can't do the relocate in the v11 with that DB. A command "db2 catalog db on /system/metalog/" doesn't work either.
I have the relocate.cfg file that should work, but all my commands go against the v11 of course since that is the default db now.
My idea was:
db2 catalog db testdb on /system/testdb/metalog/
db2 upgrade db testdb
What must I do to do the relocate and then the upgrade so I can use that DB in v11.
This is what I did to get the correct folder structur:
mkdir -p data/testdb/NODE0000
mkdir -p metalog/testdb
mkdir -p /db2/backup/testdb/testdb1/archivlogfiles/
mv metalog/olddb/NODE0000/ metalog/testdb/
mkdir data/testdb/NODE0000/TESTDB1
mv data/testdb/NODE0000/OLDDB1/ data/testdb/NODE0000/TESTDB1/
Here is the relocate.cfg
DB_NAME=OLDDB1,TESTDB1
DB_PATH=/db2/olddb/data/olddb1/metalog/,/db2/testdb/data/testdb/testdb1/metalog
INSTANCE=olddb,testdb
STORAGE_PATH=/db2/olddb/data/olddb1/data/,/db2/testdb/data/testdb/testdb1/data/
LOG_DIR=/db2/olddb/data/olddb1/metalog/olddb/NODE0000/SQL00001/LOGSTREAM0000/,/db2/testdb/data/testdb/testdb1/metalog/testdb/NODE0000/SQL00001/
LOGARCHMETH1=DISK:/db2/backup/testdb/testdb1/archivlogfiles/
CONT_PATH=/db2/olddb/data/olddb1/data/olddb1_TS_32PART.dbf,/db2/testdb/data/testdb/testdb1/data/olddb1_TS_32PART.dbf
This is the relocate log:
Logging started at Mon May 4 11:29:10 2020
Input file: /db2/testdb/scripts/relocate_olddb1.cfg
Opening configuration file.
-> File: "/db2/testdb/scripts/relocate_olddb1.cfg"
Changes requested:
-> Database name:
Old: "olddb1"
New: "testdb1"
-> Database path:
Old: "/db2/olddb/data/olddb1/metalog/"
New: "/db2/testdb/data/testdb/testdb1/metalog/"
-> Instance name:
Old: "olddb"
New: "testdb"
-> Node number: 0
-> Log directory:
Old: "/db2/testdb/data/testdb/testdb1/metalog/testdb/NODE0000/SQL00001/"
New: "/db2/testdb/data/testdb/testdb1/metalog/testdb/NODE0000/SQL00001/"
-> Container paths:
Old: "/db2/olddb/data/olddb1/data/olddb1_TS_32PART.dbf"
New: "/db2/testdb/data/testdb/testdb1/data/olddb1_TS_32PART.dbf"
-> Storage paths:
Old: "/db2/olddb/data/olddb1/data"
New: "/db2/testdb/data/testdb/testdb1/data"
SD mode: no
** PASS #1: Verifying Files and Structures **
Opening the local directory file.
-> File: "/db2/testdb/data/testdb/testdb1/metalog/testdb/NODE0000/sqldbdir/sqldbdir"
Reading directory header.
Reading hash offset table.
Reading 1 entries into memory.
Opening the global log control file
ERROR: Unable to open global log control file.
Path = "/db2/testdb/data/testdb/testdb1/metalog/testdb/NODE0000/SQL00001/"
DB2 RC = 0x801008dc
ERROR: Failed to initialize member configuration information.
DB2 RC = 0x801008dc
Exiting with RC = 1.
Logging stopped at Mon May 4 11:29:10 2020
My db2level is set to db2 v11.1.4.4.
Thank you for your help.

Querying Samba AD server with ldapsearch fails with ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)

I'm trying to leverage my existing (fully configured and working) Samba AD DC as authentication for XWiki, and other apps.
As such, I'm first trying to do a successful ldapsearch from the XWiki server.
The following command works on the Samba server, but not on the XWiki client:
ubuntu#xwiki:~$ ldapsearch -x -LLL -E pr=200/noprompt -H ldaps://10.0.1.191/ -D "CN=Administrator,CN=Users,DC=ad,DC=nitssolutions,DC=com" -w 'SambaNovi2018' -b 'DC=ad,DC=nitssolutions,DC=com' -s sub '(sAMAccountName=*)' cn mail memberOf
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
However, if I do:
ping 10.0.1.191
I get:
ubuntu#xwiki:~$ ping 10.0.1.191
PING 10.0.1.191 (10.0.1.191) 56(84) bytes of data.
64 bytes from 10.0.1.191: icmp_seq=1 ttl=64 time=135 ms
64 bytes from 10.0.1.191: icmp_seq=2 ttl=64 time=138 ms
64 bytes from 10.0.1.191: icmp_seq=3 ttl=64 time=146 ms
^C
--- 10.0.1.191 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 135.741/140.317/146.970/4.832 ms
and if I do:
telnet 10.0.1.191 636
I'm able to connect.
EDIT: Additional information:
I added a -d 1 to the ldapsearch command line, and now I get:
ubuntu#xwiki:~$ ldapsearch -d 1 -x -LLL -E pr=200/noprompt -H ldaps://10.0.1.191/ -D "CN=Administrator,CN=Users,DC=ad,DC=nitssolutions,DC=com" -w 'SambaNovi2018' -b 'DC=ad,DC=nitssolutions,DC=com' -s sub '(sAMAccountName=*)' cn mail memberOf
ldap_url_parse_ext(ldaps://10.0.1.191/)
ldap_create
ldap_url_parse_ext(ldaps://10.0.1.191:636/??base)
ldap_sasl_bind
ldap_send_initial_request
ldap_new_connection 1 1 0
ldap_int_open_connection
ldap_connect_to_host: TCP 10.0.1.191:636
ldap_new_socket: 3
ldap_prepare_socket: 3
ldap_connect_to_host: Trying 10.0.1.191:636
ldap_pvt_connect: fd: 3 tm: -1 async: 0
attempting to connect:
connect success
TLS: peer cert untrusted or revoked (0x42)
TLS: can't connect: (unknown error code).
ldap_err2string
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
Note, in particular, this line:
TLS: peer cert untrusted or revoked (0x42)
I'm going to try researching this error further, but as of now, I'm still stuck...
EDIT2: Still more additional information:
When I run this command, with the -d 1 parameter on the Samba server, the command works, in spite of having the:
TLS: peer cert untrusted or revoked (0x42)
in the debug output....
Continuing to dig....
Help?
And here I go, answering my own question again...sigh. I should post here more often. Helps me clearly lay out the problem, which inevitably leads to finding a solution.
Anyhow, the solution was:
I had a file, /etc/ldap/ldap.conf on my sambadc machine as well as my xwiki client machine, but the content differed.
The sambadc machine had:
#
# LDAP Defaults
#
# See ldap.conf(5) for details
# This file should be world readable but not world writable.
#BASE dc=example,dc=com
#URI ldap://ldap.example.com ldap://ldap-master.example.com:666
#SIZELIMIT 12
#TIMELIMIT 15
#DEREF never
# TLS certificates (needed for GnuTLS)
#TLS_CACERT /etc/ssl/certs/ca-certificates.crt
TLS_REQCERT allow
Which worked.
But, my xwiki machine had:
#
# LDAP Defaults
#
# See ldap.conf(5) for details
# This file should be world readable but not world writable.
#BASE dc=example,dc=com
#URI ldap://ldap.example.com ldap://ldap-master.example.com:666
#SIZELIMIT 12
#TIMELIMIT 15
#DEREF never
# TLS certificates (needed for GnuTLS)
TLS_CACERT /etc/ssl/certs/ca-certificates.crt
which failed.
When I commented out the TLS_CACERT line, and added the TLS_REQCERT line, it all started working as expected.

Unable to execute unix command using popen

I am trying to run the Unix command from Load Runner using popen. I tried step by step approach below are they
Step 1 :
I first tried logging into the Jump Server
fp = popen("plink.exe -ssh -l username -pw password Servername", "r");
Output using above command in Load runner :
Last login: Thu Jan 11 11:11:11 2018 from some.machine.net
Output manually using Putty :
Using username "abcdef".
Last login: Thu Jan 11 11:11:11 2018 from some.machine.net
abcdef#mymachine:~>
I successfully able to capture the desired result
Step 2 : I want to execute ls command once I am logged into the Jump Server, so I appended the ls command to the above command in the popen
fp = popen("plink.exe -ssh -l username -pw password Servername ls", "r");
then no output is produced and I get an error
In step 1 I am able to capture the output which is "Last login: Thu Jan 11 11:11:11 2018 from some.machine.net"
But in step 2 when I am trying to execute the ls command just by appending in the same popen statement I am getting error.
My objective is to read the output of the ls command
some_command is not appended.
popen("plink.exe -ssh -l username -pw password Servername some_command", "r")
should be changed into
popen("plink.exe -ssh -l username -pw password Servername ${some_command}", "r")

How to use Perl DBI for SQL Server backup

I am trying to backup a Sql Server database via Perl DBI. Calling "backup database" via do() runs but usually does not produce a backup. Calling do() creates a backup when ODBC tracing is enabled. Calling prepare() and execute() fails.
I am using ActiveState Perl on Windows 7 Professional and Sql Server 2008 R2.
Here is a link to download source code and various logs
http://www.fileswap.com/dl/4VnYbCdk6R/ToZip.zip.html
(Click on slow download)
Here is the summary of logs
BothTraces made 3 backups but program aborted
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 09:39 perlEasy.bak
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 09:39 perlHard.bak
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 09:38 queryOS.bak
NoTracing made 1 backup, program aborted
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 10:15 queryOS.bak
DbiTrace made 1 backup, program aborted
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 10:19 queryOS.bak
OdbcTrace made 3 backup but program aborted
-rwx------+ 1 SYSTEM SYSTEM 159744 Jan 16 10:21 perlEasy.bak
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 10:21 perlHard.bak
-rwx------+ 1 SYSTEM SYSTEM 160256 Jan 16 10:21 queryOS.bak
Here's my program:
#!perl -w
#try to use DBI for SQL Server backup
#connect to database server
use v5.14; #enable modern Perl
use DBI; #database interface
my $dbHandle = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=DavidZ") or die; #dbi prints a detailed error message
$dbHandle->{RaiseError} = 1; #enable failure on DBI problems; obviates the need for "or die" with every DBI call
$dbHandle->{PrintError} = 0; #don't duplicate error messages
#enable debugging
$dbHandle->trace(1);
$dbHandle->{odbc_trace} = 1; #not helpful
$dbHandle->{odbc_trace_file} = 'C:\David\dump\tracer.file'; #not helpful
#run a SQL command to verify connection, write a note to ERRORLOG
$dbHandle->do ('use master');
$dbHandle->do ("raiserror ('New run of backup.pl', 0, 0) with log");
say 'Verified database connection';
#backup commands
my $perlEasy = "backup database dz to disk='C:\\David\\dump\\perlEasy.bak'";
my $perlHard = "backup database dz to disk='C:\\David\\dump\\perlHard.bak'";
my $queryOS = "backup database dz to disk='C:\\David\\dump\\queryOS.bak'";
#make a backup via sqlcmd. this works
my $sysCmd = "sqlcmd -Q \"$queryOS\" ";
system ($sysCmd) == 0
or die "The following system command failed: $sysCmd \n";
say 'Created backup via sqlcmd';
#try to make a backup via DBI
$dbHandle->do ($perlEasy); #runs silently but does not produce a backup file
say 'Created backup the easy way';
#more complicated DBI method
my $stHandle = $dbHandle->prepare($perlHard);
$stHandle->execute(); #statement starts a backup then fails, no furter code is executed
do
{
#print dbi results
say "DBI reports $DBI::errstr";
while (my #row = $stHandle->fetchrow_array()) #recommended by someone, but makes no sense for a backup
{ say "Returned values: #row" } #recommended by someone, but makes no sense for a backup
} while ($stHandle->{odbc_more_results});
say 'Created backup the hard way';
#program completion
say 'Program completed successfully';
exit 0;
There is nothing wrong with the Perl code you show. However, the ODBC trace file shows that DBD::ODBC made these calls just before the error:
SQLPrepare backup database dz to disk='C:\David\dump\perlHard.bak'
SQLExecute returns SQL_SUCCESS_WITH_INFO and
Processed 208 pages for database 'dz', file 'dz_test' on file 1. (4035)
then a few calls for various handles to SQLErrorW
SQLRowCount returns ok and -1 for row count
SQLNumResultCols returns SQL_ERROR and ]Invalid cursor state
I cannot for the life of me see how this is an invalid cursor state (look the valid state transitions for ODBC up yourself) so I'd have to say this looks like a bug in the SQL Server ODBC driver you are using. You could try getting a newer one or use the SQL Server native client driver instead (you've probably got both already).
You can ignore the errors in your sql server log as they are correct, error 1235 is ERROR_REQUEST_ABORTED which it was.

disable NOTICES in psql output

How do I stop psql (PostgreSQL client) from outputting notices? e.g.
psql:schema/auth.sql:20: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
In my opinion a program should be silent unless it has an error, or some other reason to output stuff.
SET client_min_messages TO WARNING;
That could be set only for the session or made persistent with ALTER ROLE or ALTER DATABASE.
Or you could put that in your ".psqlrc".
Probably the most comprehensive explanation is on Peter Eisentrauts blog entry here (Archive)
I would strongly encourage that the original blog be studied and digested but the final recommendation is something like :
PGOPTIONS='--client-min-messages=warning' psql -X -q -a -1 -v ON_ERROR_STOP=1 --pset pager=off -d mydb -f script.sql
Use --quiet when you start psql.
A notice is not useless, but that's my point of view.
It can be set in the global postgresql.conf file as well with modifiying the client_min_messages parameter.
Example:
client_min_messages = warning
I tried the various solutions suggested (and permutations thereof) suggested in this thread, but I was unable to completely suppress PSQL output / notifications.
I am executing a claws2postgres.sh BASH script that does some preliminary processing then calls/executes a PSQL .sql script, to insert 1000's of entries into PostgreSQL.
...
PGOPTIONS="-c client_min_messages=error"
psql -d claws_db -f claws2postgres.sql
Output
[victoria#victoria bash]$ ./claws2postgres.sh
pg_terminate_backend
----------------------
DROP DATABASE
CREATE DATABASE
You are now connected to database "claws_db" as user "victoria".
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 1
UPDATE 1
UPDATE 1
Dropping tmp_table
DROP TABLE
You are now connected to database "claws_db" as user "victoria".
psql:/mnt/Vancouver/projects/ie/claws/src/sql/claws2postgres.sql:33: NOTICE: 42P07: relation "claws_table" already exists, skipping
LOCATION: transformCreateStmt, parse_utilcmd.c:206
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 2
UPDATE 2
UPDATE 2
Dropping tmp_table
DROP TABLE
[ ... snip ... ]
SOLUTION
Note this modified PSQL line, where I redirect the psql output:
psql -d claws_db -f $SRC_DIR/sql/claws2postgres.sql &>> /tmp/pg_output.txt
The &>> /tmp/pg_output.txt redirect appends all output to an output file, that can also serve as a log file.
BASH terminal output
[victoria#victoria bash]$ time ./claws2postgres.sh
pg_terminate_backend
----------------------
DROP DATABASE
CREATE DATABASE
2:40:54 ## 2 h 41 min
[victoria#victoria bash]$
Monitor progress:
In another terminal, execute
PID=$(pgrep -l -f claws2postgres.sh | grep claws | awk '{ print $1 }'); while kill -0 $PID >/dev/null 2>&1; do NOW=$(date); progress=$(cat /tmp/pg_output.txt | wc -l); printf "\t%s: %i lines\n" "$NOW" $progress; sleep 60; done; for i in seq{1..5}; do aplay 2>/dev/null /mnt/Vancouver/programming/scripts/phaser.wav && sleep 0.5; done
...
Sun 28 Apr 2019 08:18:43 PM PDT: 99263 lines
Sun 28 Apr 2019 08:19:43 PM PDT: 99391 lines
Sun 28 Apr 2019 08:20:43 PM PDT: 99537 lines
[victoria#victoria output]$
pgrep -l -f claws2postgres.sh | grep claws | awk '{ print $1 }' gets the script PID, assigned to $PID
while kill -0 $PID >/dev/null 2>&1; do ... : while that script is running, do ...
cat /tmp/pg_output.txt | wc -l : use the output file line count as a progress indicator
when done, notify by playing phaser.wav 5 times
phaser.wav: https://persagen.com/files/misc/phaser.wav
Output file:
[victoria#victoria ~]$ head -n22 /tmp/pg_output.txt
You are now connected to database "claws_db" as user "victoria".
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 1
UPDATE 1
UPDATE 1
Dropping tmp_table
DROP TABLE
You are now connected to database "claws_db" as user "victoria".
psql:/mnt/Vancouver/projects/ie/claws/src/sql/claws2postgres.sql:33: NOTICE: 42P07: relation "claws_table" already exists, skipping
LOCATION: transformCreateStmt, parse_utilcmd.c:206
CREATE TABLE
SELECT 1
INSERT 0 1
UPDATE 2
UPDATE 2
UPDATE 2
Dropping tmp_table
DROP TABLE
References
[re: solution, above] PSQL: How can I prevent any output on the command line?
[re: this SO thread] disable NOTICES in psql output
[related SO thread] Postgresql - is there a way to disable the display of INSERT statements when reading in from a file?
[relevant to solution] https://askubuntu.com/questions/350208/what-does-2-dev-null-mean
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
If you don't specify a number then the standard output stream is assumed but you can also redirect errors
> file redirects stdout to file
1> file redirects stdout to file
2> file redirects stderr to file
&> file redirects stdout and stderr to file
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
Offering a suggestion that is useful for a specific scenario I had:
Windows command shell calls psql.exe call to execute one essential SQL command
Only want to see warnings or errors, and suppress NOTICES
Example:
psql.exe -c "SET client_min_messages TO WARNING; DROP TABLE IF EXISTS mytab CASCADE"
(I was unable to make things work with PGOPTIONS as a Windows environment variable--couldn't work out the right syntax. Tried multiple approaches from different posts.)

Resources