Home Explore Blog CI



postgresql

80th chunk of `doc/src/sgml/ref/psql-ref.sgml`
c1ea3d6001996abb7c5a1697d19af7c34711179aa5a1d6530000000100000ec5
 <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=&gt; <userinput>CREATE TABLE my_table (</userinput>
testdb(&gt; <userinput> first integer not null default 0,</userinput>
testdb(&gt; <userinput> second text)</userinput>
testdb-&gt; <userinput>;</userinput>
CREATE TABLE
</programlisting>
  Now look at the table definition again:
<programlisting>
testdb=&gt; <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=&gt; <userinput>\set PROMPT1 '%n@%m %~%R%# '</userinput>
peter@localhost testdb=&gt;
</programlisting>
  Let's assume you have filled the table with data and want to take a
  look at it:
<programlisting>
peter@localhost testdb=&gt; 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=&gt; <userinput>\pset border 2</userinput>
Border style is 2.
peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
+-------+--------+
| first | second |
+-------+--------+
|     1 | one    |
|     2 | two    |
|     3 | three  |
|     4 | four   |
+-------+--------+
(4 rows)

peter@localhost testdb=&gt; <userinput>\pset border 0</userinput>
Border style is 0.
peter@localhost testdb=&gt; <userinput>SELECT * FROM my_table;</userinput>
first second
----- ------
    1 one
    2 two
    3 three
    4 four
(4 rows)

peter@localhost testdb=&gt; <userinput>\pset border 1</userinput>
Border style is 1.
peter@localhost testdb=&gt; <userinput>\pset format csv</userinput>
Output format is csv.
peter@localhost testdb=&gt; <userinput>\pset tuples_only</userinput>
Tuples only is on.
peter@localhost testdb=&gt; <userinput>SELECT second, first FROM my_table;</userinput>
one,1
two,2
three,3
four,4
peter@localhost testdb=&gt; <userinput>\pset format unaligned</userinput>
Output format is unaligned.
peter@localhost testdb=&gt; <userinput>\pset fieldsep '\t'</userinput>
Field separator is "    ".
peter@localhost testdb=&gt; <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=&gt; <userinput>\a

Title: psql Windows Encoding and Examples
Summary
psql on Windows requires special attention to encoding due to differences between the console and the system. The correct console code page must be set using 'cmd.exe /c chcp 1252' (or the appropriate code page) and the console font should be set to Lucida Console. The section then provides examples of creating a table, viewing its definition, changing the prompt, querying data, and using the \pset command to change the output format and display of tables.