1.4. Accessing a Database

|  1.4. Accessing a Database   |                         |                          |                  |                          |
|:----------------------------:|-------------------------|--------------------------|------------------|--------------------------|
|[Prev](tutorial-createdb.html)|[Up](tutorial-start.html)|Chapter 1. Getting Started|[Home](index.html)| [Next](tutorial-sql.html)|

---

## 1.4. Accessing a Database

[]()

 Once you have created 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 pgAdmin or an office suite with ODBC or JDBC support 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 [Part IV](client-interfaces.html).

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

```
$ psql mydb

```

 If you do not supply the database name then it will default to your user account name. You already discovered this scheme in the previous section using `createdb`.

 In `psql`, you will be greeted with the following message:

```
psql (14.2)
Type "help" for help.

mydb=>

```

[]() The last line could also be:

```
mydb=#

```

 That would mean you are a database superuser, which is most likely the case if you installed the PostgreSQL instance yourself. Being a superuser means that you are not subject to access controls. For the purposes of this tutorial that is not important.

 If you encounter problems starting `psql` then go back to the previous section. The diagnostics of `createdb` and `psql` are similar, and if the former worked the latter should work as well.

 The last line printed out by `psql` is the prompt, and it indicates that `psql` is listening to you and that you can type SQL queries into a work space maintained by `psql`. Try out these commands: []()

```
mydb=> SELECT version();
                                         version
-------------------------------------------------------------------​-----------------------
 PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)

mydb=> SELECT current_date;
    date
------------
 2016-01-07
(1 row)

mydb=> SELECT 2 + 2;
 ?column?
----------
        4
(1 row)

```

 The `psql` program has a number of internal commands that are not SQL commands. They begin with the backslash character, “`\`”. For example, you can get help on the syntax of various PostgreSQL SQL commands by typing:

```
mydb=> \h

```

 To get out of `psql`, type:

```
mydb=> \q

```

 and `psql` will quit and return you to your command shell. (For more internal commands, type `\?` at the `psql` prompt.) The full capabilities of `psql` are documented in [psql](app-psql.html). In this tutorial we will not use these features explicitly, but you can use them yourself when it is helpful.

---

|[Prev](tutorial-createdb.html)|[Up](tutorial-start.html)|  [Next](tutorial-sql.html) |
|:-----------------------------|:-----------------------:|---------------------------:|
|   1.3. Creating a Database   |   [Home](index.html)    | Chapter 2. The SQL Language|

 // \<![CDATA[ \<-- For SVG support if ('WebSocket' in window) { (function() { function refreshCSS() { var sheets = [].slice.call(document.getElementsByTagName("link")); var head = document.getElementsByTagName("head")[0]; for (var i = 0; i \< sheets.length; ++i) { var elem = sheets[i]; head.removeChild(elem); var rel = elem.rel; if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") { var url = elem.href.replace(/(&|\\?)\_cacheOverride=\\d+/, ''); elem.href = url + (url.indexOf('?') \>= 0 ? '&' : '?') + '\_cacheOverride=' + (new Date().valueOf()); } head.appendChild(elem); } } var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; var address = protocol + window.location.host + window.location.pathname + '/ws'; var socket = new WebSocket(address); socket.onmessage = function(msg) { if (msg.data == 'reload') window.location.reload(); else if (msg.data == 'refreshcss') refreshCSS(); }; console.log('Live reload enabled.'); })(); } // ]]\>