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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>1.4.Accessing a Database</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="tutorial-start.html" title="Chapter1.Getting Started">
<link rel="prev" href="tutorial-createdb.html" title="1.3.Creating a Database">
<link rel="next" href="tutorial-sql.html" title="Chapter2.The SQL Language">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tutorial-accessdb"></a>1.4.Accessing a Database</h2></div></div></div>
<a name="id567169"></a><p> Once you have created a database, you can access it by:
</p>
<div class="itemizedlist"><ul type="bullet" compact>
<li 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 style="list-style-type: disc"><p> Using an existing graphical frontend tool like
<span class="application">PgAccess</span> or an office suite with
<acronym class="acronym">ODBC</acronym> support to create and manipulate a
database. These possibilities are not covered in this
tutorial.
</p></li>
<li 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 href="client-interfaces.html" title="PartIV.Client Interfaces">PartIV, “Client Interfaces”</a>.
</p></li>
</ul></div>
<p>
You probably want to start up <code class="command">psql</code>, to try out
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 leave off the database name then it will default to your
user account name. You already discovered this scheme in the
previous section.
</p>
<p> In <code class="command">psql</code>, you will be greeted with the following
message:
</p>
<pre class="screen">Welcome to psql 8.1.4, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
mydb=></pre>
<p>
<a name="id567298"></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 <span class="productname">PostgreSQL</span>
yourself. Being a superuser means that you are not subject to
access controls. For the purpose of this tutorial this is not of
importance.
</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 name="id567378"></a>
</p>
<pre class="screen"><code class="prompt">mydb=></code> <strong class="userinput"><code>SELECT version();</code></strong>
version
----------------------------------------------------------------
PostgreSQL 8.1.4 on i586-pc-linux-gnu, compiled by GCC 2.96
(1 row)
<code class="prompt">mydb=></code> <strong class="userinput"><code>SELECT current_date;</code></strong>
date
------------
2002-08-31
(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"><code class="literal">\</code></span>”. Some of these
commands were listed in the welcome message. 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 href="app-psql.html" title="psql"><span class="refentrytitle"><a name="app-psql-title"></a><span class="application">psql</span></span></a>. If <span class="productname">PostgreSQL</span> is
installed correctly you can also type <code class="literal">man psql</code>
at the operating system shell prompt to see the documentation. In
this tutorial we will not use these features explicitly, but you
can use them yourself when you see fit.
</p>
</div></body>
</html>
|