POSTGRESQL-ESSENTIALS practice question 4 of 15

You are connected to a PostgreSQL database. After executing a complex query an error message is displayed. You want to edit and rerun the previously executed query. Which command can be used to edit the query buffer?

Full set: 71 questions
Answer options
A: L
B: R
C: \e Correct answer
D: e
Correct Answer:
C. \e
Explanation

The psql interactive terminal provides meta-commands for managing the session and queries. The \e (or \edit) command opens the current query buffer in an external editor, which is determined by the PSQLEDITOR, EDITOR, or VISUAL environment variables. After a query is executed, it remains in the buffer. Using \e allows the user to modify this last query, save the changes, and exit the editor. The updated query is then loaded back into the psql buffer, ready to be executed again with the \g command.

Why Incorrect

A. L: The \l command is used to list all databases in the PostgreSQL server instance, not for editing queries.
B. R: The \r command resets (clears) the query buffer entirely, which is the opposite of editing the previous query.
D. e: The character e by itself is not a valid psql meta-command. Meta-commands must be prefixed with a backslash (\). ---

References

1. PostgreSQL Official Documentation: The psql documentation explicitly lists and describes the \e meta-command.

Source: PostgreSQL 16 Documentation, Chapter 55. psql, Section 55.3. Meta-Commands.

Reference: In the table of meta-commands, the entry for \edit states: "Edits the current query buffer... with an external editor. It is also executed if you just type \e."

2. University Courseware: Reputable university database courses introduce psql as a fundamental tool and cover its essential commands.

Source: University of California, Berkeley, CS 186: Introduction to Database Systems, Lab 1: SQL.

Reference: The lab materials typically include a section on "psql commands," where \e is introduced as the command to edit the last executed query in an editor like vim or emacs.

3. Academic Publication (Textbook): Foundational database textbooks often include practical guides for interacting with specific database systems.

Source: Ramakrishnan, R., & Gehrke, J. (2003). Database Management Systems (3rd ed.). McGraw-Hill.

Reference: Chapter on "The SQL Query Language," in sections covering database client tools, the functionality to recall and edit previous commands using features like \e in psql is described as a key productivity feature.

Scroll to Top