Accessing a Database

PostgreSQL 7.2 Documentation
Prev | Chapter 10. Managing a Database | Next

# 10.2. Accessing a Database

Once you have constructed a database, you can access it by:

- Running the PostgreSQL interactive terminal program, called psql, which allows you to interactively enter, edit, and execute SQL commands.

- Using an existing graphical frontend tool like PgAccess or ApplixWare (via ODBC) to create and manipulate a database. These possibilities are not covered in this tutorial.

- Writing a custom application, using one of the several available language bindings. These possibilities are discussed further in The PostgreSQL Programmer's Guide.

You probably want to start up psql, to try out the examples in this manual. It can be activated for the mydb database by typing the command:

    % psql mydb

You will be greeted with the following message:

    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

    mydb=>

This prompt indicates that psql is listening to you and that you can type SQL queries into a work space maintained by the terminal monitor. The psql program itself responds to special commands that begin with the backslash character, `\`. For example, you can get help on the syntax of various PostgreSQL SQL commands by typing:

    mydb=> \h

Once you have finished entering your queries into the work space, you can pass the contents of the work space to the PostgreSQL server by typing:

    mydb=> \g

This tells the server to process the query. If you terminate your query with a semicolon, the `\g` is not necessary. psql will automatically process semicolon terminated queries. To read queries from a file, say `myFile`, instead of entering them interactively, type:

    mydb=> \i myFile

To get out of psql and return to Unix, type

    mydb=> \q

and psql will quit and return you to your command shell. (For more escape codes, type \? at the psql prompt.) White space (i.e., spaces, tabs and newlines) may be used freely in SQL queries. Single-line comments are denoted by `--`. Everything after the dashes up to the end of the line is ignored. Multiple-line comments, and comments within a line, are denoted by `/* ... */`.

Prev | Home | Next
Managing a Database | Up | Destroying a Database