To enter queries directly into the prompt, open psql and make sure you are connected to the correct database (and logged in as the correct user). You should be presented with a prompt that, by default, is set to display the name of the database you are currently connected to. The prompt should look something like the following immediately after opening psql :
Example 4-3. The psql Prompt
Welcome to psql, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help on internal slash commands \g or terminate with semicolon to execute query \q to quit testdb=#
To pass SQL statements to PostgreSQL, just type them into the prompt. Anything you type (barring a slash command) will be queued until you terminate the query with a semi-colon, even if you start a new line. This allows you to spread query statements across multiple lines. Examine the following example to see how this is done.
Example 4-4. Entering Statements into psql
testdb=# SELECT * FROM employees testdb-# WHERE firstname = 'Michael';
This will return a table of matching data. This query could be broken up over multiple lines to improve readability even more and psql still would not send it until the terminating semi-colon was sent. What isn't displayed by this example is that the prompt will show you any end-character that you've left open, such as parenthesis. If you were to issue a CREATE TABLE command to start a statement, then hit enter to start a new line for readability purposes, you would see a prompt similar to what is displayed in the following example.
Example 4-5. Leaving end-characters open
testdb=# CREATE TABLE employees ( testdb(#
At this point you could continue the statement. The psql prompt is informing you of the open parenthesis by inserting an open parenthesis symbol into the prompt.