]> begriffs open source - ai-pg/blob - full-docs/txt/tutorial-accessdb.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / tutorial-accessdb.txt
1
2 1.4. Accessing a Database #
3
4    Once you have created a database, you can access it by:
5      * Running the PostgreSQL interactive terminal program, called psql,
6        which allows you to interactively enter, edit, and execute SQL
7        commands.
8      * Using an existing graphical frontend tool like pgAdmin or an office
9        suite with ODBC or JDBC support to create and manipulate a
10        database. These possibilities are not covered in this tutorial.
11      * Writing a custom application, using one of the several available
12        language bindings. These possibilities are discussed further in
13        Part IV.
14
15    You probably want to start up psql to try the examples in this
16    tutorial. It can be activated for the mydb database by typing the
17    command:
18 $ psql mydb
19
20    If you do not supply the database name then it will default to your
21    user account name. You already discovered this scheme in the previous
22    section using createdb.
23
24    In psql, you will be greeted with the following message:
25 psql (18.0)
26 Type "help" for help.
27
28 mydb=>
29
30    The last line could also be:
31 mydb=#
32
33    That would mean you are a database superuser, which is most likely the
34    case if you installed the PostgreSQL instance yourself. Being a
35    superuser means that you are not subject to access controls. For the
36    purposes of this tutorial that is not important.
37
38    If you encounter problems starting psql then go back to the previous
39    section. The diagnostics of createdb and psql are similar, and if the
40    former worked the latter should work as well.
41
42    The last line printed out by psql is the prompt, and it indicates that
43    psql is listening to you and that you can type SQL queries into a work
44    space maintained by psql. Try out these commands:
45 mydb=> SELECT version();
46                                          version
47 -------------------------------------------------------------------​------------
48 -----------
49  PostgreSQL 18.0 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2
50 , 64-bit
51 (1 row)
52
53 mydb=> SELECT current_date;
54     date
55 ------------
56  2016-01-07
57 (1 row)
58
59 mydb=> SELECT 2 + 2;
60  ?column?
61 ----------
62         4
63 (1 row)
64
65    The psql program has a number of internal commands that are not SQL
66    commands. They begin with the backslash character, “\”. For example,
67    you can get help on the syntax of various PostgreSQL SQL commands by
68    typing:
69 mydb=> \h
70
71    To get out of psql, type:
72 mydb=> \q
73
74    and psql will quit and return you to your command shell. (For more
75    internal commands, type \? at the psql prompt.) The full capabilities
76    of psql are documented in psql. In this tutorial we will not use these
77    features explicitly, but you can use them yourself when it is helpful.