<application>psql</application> is built as a <quote>console
application</quote>. Since the Windows console windows use a different
encoding than the rest of the system, you must take special care
when using 8-bit characters within <application>psql</application>.
If <application>psql</application> detects a problematic
console code page, it will warn you at startup. To change the
console code page, two things are necessary:
<itemizedlist>
<listitem>
<para>
Set the code page by entering <userinput>cmd.exe /c chcp
1252</userinput>. (1252 is a code page that is appropriate for
German; replace it with your value.) If you are using Cygwin,
you can put this command in <filename>/etc/profile</filename>.
</para>
</listitem>
<listitem>
<para>
Set the console font to <literal>Lucida Console</literal>, because the
raster font does not work with the ANSI code page.
</para>
</listitem>
</itemizedlist></para>
</refsect1>
<refsect1 id="app-psql-examples" xreflabel="Examples">
<title>Examples</title>
<para>
The first example shows how to spread a command over several lines of
input. Notice the changing prompt:
<programlisting>
testdb=> <userinput>CREATE TABLE my_table (</userinput>
testdb(> <userinput> first integer not null default 0,</userinput>
testdb(> <userinput> second text)</userinput>
testdb-> <userinput>;</userinput>
CREATE TABLE
</programlisting>
Now look at the table definition again:
<programlisting>
testdb=> <userinput>\d my_table</userinput>
Table "public.my_table"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
first | integer | | not null | 0
second | text | | |
</programlisting>
Now we change the prompt to something more interesting:
<programlisting>
testdb=> <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
peter@localhost testdb=>
</programlisting>
Let's assume you have filled the table with data and want to take a
look at it:
<programlisting>
peter@localhost testdb=> SELECT * FROM my_table;
first | second
-------+--------
1 | one
2 | two
3 | three
4 | four
(4 rows)
</programlisting>
You can display tables in different ways by using the
<command>\pset</command> command:
<programlisting>
peter@localhost testdb=> <userinput>\pset border 2</userinput>
Border style is 2.
peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
+-------+--------+
| first | second |
+-------+--------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
+-------+--------+
(4 rows)
peter@localhost testdb=> <userinput>\pset border 0</userinput>
Border style is 0.
peter@localhost testdb=> <userinput>SELECT * FROM my_table;</userinput>
first second
----- ------
1 one
2 two
3 three
4 four
(4 rows)
peter@localhost testdb=> <userinput>\pset border 1</userinput>
Border style is 1.
peter@localhost testdb=> <userinput>\pset format csv</userinput>
Output format is csv.
peter@localhost testdb=> <userinput>\pset tuples_only</userinput>
Tuples only is on.
peter@localhost testdb=> <userinput>SELECT second, first FROM my_table;</userinput>
one,1
two,2
three,3
four,4
peter@localhost testdb=> <userinput>\pset format unaligned</userinput>
Output format is unaligned.
peter@localhost testdb=> <userinput>\pset fieldsep '\t'</userinput>
Field separator is " ".
peter@localhost testdb=> <userinput>SELECT second, first FROM my_table;</userinput>
one 1
two 2
three 3
four 4
</programlisting>
Alternatively, use the short commands:
<programlisting>
peter@localhost testdb=> <userinput>\a