Congratulations. You have successfully connected to the POSTGRESQL
server. You can now issue commands and receive replies from the server.
Let's try one. Type SELECT CURRENT_USER;
and press Enter (see Figure ).
test=> SELECT CURRENT_USER;
getpgusername
---------------
postgres
(1 row)
test=>
If you make a mistake, just press Backspace and retype the command. It should show your login name underneath the dashed line. This example shows the login name of postgres. The word getpgusername is a column label. The server also reports that it has returned one row of data. The line test=> tells you that the server has finished its current task and is waiting for the next database query.
Let's try another one. At the test=> prompt, type SELECT CURRENT_TIMESTAMP ; and press Enter. You should see the current date and time. Each time you execute the query, the server will report the current time to you.
Typing in the query buffer is similar to typing at an operating system command prompt. However, at an operating system command prompt, Enter completes each command. In psql, commands are completed only when you enter a semicolon (;) or backslash-g (\g).
As an example, let's do SELECT 1 + 3; but in a different
way. See Figure .
7.2
test=> SELECT
test-> 1 + 3
test-> ;
?column?
----------
4
(1 row)
test=>
Notice that the query is spread over three lines. The prompt changed from => on the first line to -> on the second line to indicate that the query was continued. The semicolon told psql to send the query to the server. We could have easily replaced the semicolon with backslash-g. I do not recommend that you type queries as ugly as this one, but longer queries will benefit by being spread over multiple lines. You might notice that the query is in uppercase. Unless you are typing a string in quotes, the POSTGRESQL server does not care whether words are uppercase or lowercase. For clarity, I recommend you enter words special to POSTGRESQL in uppercase.
Try some queries on your own involving arithmetic. Each computation must start with the word SELECT, then your computation, and finally a semicolon or backslash-g. For example, SELECT 4 * 10; would return 40. Addition is performed using a plus symbol (+), subtraction using a minus symbol (-), multiplication using an asterisk (*), and division using a forward slash (/).
If you have readline 7.3 installed, psql will even allow you to use your arrow keys. Your left and right arrow keys allow you to move around, and the up and down arrows retrieve previously typed queries.
You can continue typing indefinitely, until you use a semicolon or
backslash-g. Everything you type will be buffered by psql
until you are ready to send the query. If you use backslash-p
(\p), you will see everything accumulated
in the query buffer. In Figure , three lines
of text are accumulated and displayed by the user using backslash-p.
test=> SELECT
test-> 2 * 10 + 1
test-> \p
SELECT
2 * 10 + 1
test-> \g
?column?
----------
21
(1 row)
test=>
After display, we use backslash-g to execute the query, which returns the value 21. This ability comes in handy with long queries.
If you do not like what you have typed, use backslash-r (\r) to reset or erase the buffer.