1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>1.4. Accessing a Database</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="tutorial-createdb.html" title="1.3. Creating a Database" /><link rel="next" href="tutorial-sql.html" title="Chapter 2. The SQL Language" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">1.4. Accessing a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-createdb.html" title="1.3. Creating a Database">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-start.html" title="Chapter 1. Getting Started">Up</a></td><th width="60%" align="center">Chapter 1. Getting Started</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Next</a></td></tr></table><hr /></div><div class="sect1" id="TUTORIAL-ACCESSDB"><div class="titlepage"><div><div><h2 class="title" style="clear: both">1.4. Accessing a Database</h2></div></div></div><a id="id-1.4.3.5.2" class="indexterm"></a><p>
Once you have created a database, you can access it by:
</p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: bullet; "><li class="listitem" style="list-style-type: disc"><p>
Running the <span class="productname">PostgreSQL</span> interactive
terminal program, called <span class="application"><em class="firstterm">psql</em></span>, which allows you
to interactively enter, edit, and execute
<acronym class="acronym">SQL</acronym> commands.
</p></li><li class="listitem" style="list-style-type: disc"><p>
Using an existing graphical frontend tool like
<span class="application">pgAdmin</span> or an office suite with
<acronym class="acronym">ODBC</acronym> or <acronym class="acronym">JDBC</acronym> support to create and manipulate a
database. These possibilities are not covered in this
tutorial.
</p></li><li class="listitem" style="list-style-type: disc"><p>
Writing a custom application, using one of the several
available language bindings. These possibilities are discussed
further in <a class="xref" href="client-interfaces.html" title="Part IV. Client Interfaces">Part IV</a>.
</p></li></ul></div><p>
You probably want to start up <code class="command">psql</code> to try
the examples in this tutorial. It can be activated for the
<code class="literal">mydb</code> database by typing the command:
</p><pre class="screen">
<code class="prompt">$</code> <strong class="userinput"><code>psql mydb</code></strong>
</pre><p>
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 <code class="command">createdb</code>.
</p><p>
In <code class="command">psql</code>, you will be greeted with the following
message:
</p><pre class="screen">
psql (15.4)
Type "help" for help.
mydb=>
</pre><p>
<a id="id-1.4.3.5.4.3" class="indexterm"></a>
The last line could also be:
</p><pre class="screen">
mydb=#
</pre><p>
That would mean you are a database superuser, which is most likely
the case if you installed the <span class="productname">PostgreSQL</span> instance
yourself. Being a superuser means that you are not subject to
access controls. For the purposes of this tutorial that is not
important.
</p><p>
If you encounter problems starting <code class="command">psql</code>
then go back to the previous section. The diagnostics of
<code class="command">createdb</code> and <code class="command">psql</code> are
similar, and if the former worked the latter should work as well.
</p><p>
The last line printed out by <code class="command">psql</code> is the
prompt, and it indicates that <code class="command">psql</code> is listening
to you and that you can type <acronym class="acronym">SQL</acronym> queries into a
work space maintained by <code class="command">psql</code>. Try out these
commands:
<a id="id-1.4.3.5.6.5" class="indexterm"></a>
</p><pre class="screen">
<code class="prompt">mydb=></code> <strong class="userinput"><code>SELECT version();</code></strong>
version
------------------------------------------------------------------------------------------
PostgreSQL 15.4 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
<code class="prompt">mydb=></code> <strong class="userinput"><code>SELECT current_date;</code></strong>
date
------------
2016-01-07
(1 row)
<code class="prompt">mydb=></code> <strong class="userinput"><code>SELECT 2 + 2;</code></strong>
?column?
----------
4
(1 row)
</pre><p>
</p><p>
The <code class="command">psql</code> program has a number of internal
commands that are not SQL commands. They begin with the backslash
character, <span class="quote">“<span class="quote"><code class="literal">\</code></span>”</span>.
For example,
you can get help on the syntax of various
<span class="productname">PostgreSQL</span> <acronym class="acronym">SQL</acronym>
commands by typing:
</p><pre class="screen">
<code class="prompt">mydb=></code> <strong class="userinput"><code>\h</code></strong>
</pre><p>
</p><p>
To get out of <code class="command">psql</code>, type:
</p><pre class="screen">
<code class="prompt">mydb=></code> <strong class="userinput"><code>\q</code></strong>
</pre><p>
and <code class="command">psql</code> will quit and return you to your
command shell. (For more internal commands, type
<code class="literal">\?</code> at the <code class="command">psql</code> prompt.) The
full capabilities of <code class="command">psql</code> are documented in
<a class="xref" href="app-psql.html" title="psql"><span class="refentrytitle"><span class="application">psql</span></span></a>. In this tutorial we will not use these
features explicitly, but you can use them yourself when it is helpful.
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-createdb.html" title="1.3. Creating a Database">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-start.html" title="Chapter 1. Getting Started">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.3. Creating a Database </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 2. The <acronym class="acronym">SQL</acronym> Language</td></tr></table></div></body></html>
|