Sample Exam Questions

From the objective of OSS-DB Exam Silver
S2.2 Usage of server management tools (psql)

Sample Question

2.21

You want to execute a SELECT statement using psql and output the results to a CSV file, output.csv. Choose two appropriate methods.

 
  1. When starting psql, specify the --mode=csv option, and execute the following from the psql prompt:
    SELECT ... > output.csv

  2. When starting psql, specify >output.csv option to redirect the output, and then execute the following commands in the specified order:
    \format csv
    SELECT ...

  3. After starting psql, execute the following commands in the specified order:
    \o output.csv
    \pset format csv
    SELECT ....

  4. Execute the following from the command line:
    psql -c "SELECT ..." --csv > output.csv

  5. Execute the following from the command line:
    psql -c "SELECT ..." --format=csv > output.csv

※This sample exam is different from those that appear in the actual OSS-DB Exam.
2024/05/07

Answer and Explanation

psql allows you to connect to a database, execute SQL, and output the results in various formats, but by default, the output is formatted as shown below.

select * from test;
 id | val
----+-----
  1 | aaa

You must specify options to create output files in CSV format.

You can export a CSV by combining two options: not aligning and specifying the delimiter character. However, there is an easier way to do this by specifying 'format csv' to the \pset meta-command. Also, since psql allows you to specify a file to output the results to with the \o meta-command, option C will output the results of the SELECT statement in CSV format to output.csv.

The command-line options for starting psql can do the same thing, and the --csv option is the same as the meta-command \pset format csv. You can also specify the SELECT statement to be executed with the -c option and the destination file name with redirection, as in option D.

So the correct answers are C and D.