Postgres: Difference between revisions

From Leo's Notes
This page was last edited on 26 January 2020, at 19:05.
m →= Init DB: format
Line 16: Line 16:
## -W to prompt for a password
## -W to prompt for a password
## -h for hostname
## -h for hostname
## and the database name
## -p for port
## -d (optional) for the database name
}}
}}


Once in the psql console databases can be listed with {{code|\l}}:
Once in the psql console, commands are issued with a backslash.
 
Common commands are:
{|class="wikitable"
! Command
! Description
|-
| {{code|\l}}
| Lists databases
 
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
<nowiki>
<nowiki>
Line 35: Line 45:
</nowiki>
</nowiki>
}}
}}
|-
| {{code|\c}}
| Change database


A database can be selected or changed with {{code|\c}}.
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
<nowiki>
<nowiki>
Line 43: Line 55:
</nowiki>
</nowiki>
}}
}}
 
|-
Tables can be listed with {{code|\dt}}:
| {{code|\dt}}
| Describes tables of a database.
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
citrix-confdb=# \dt
citrix-confdb=# \dt
Line 55: Line 68:
(3 rows)
(3 rows)
}}
}}
|-
| {{code|\q}}
| Quits the console. Same with {{code|Ctrl+d}}
|-
| {{code|\!}}
| Executes a system command. Eg. {{code|\! ls}}
|-
| {{code|\set}}
| Sets parameters in psql. Some parameters are set in {{code|.psqlrc}}.
|}


Records can be selected. However, if the table name is in mixed case, the table name needs to be in quotes.
If the table name is in mixed case, the table name needs to be in quotes.
{{highlight|lang=terminal|code=
{{highlight|lang=terminal|code=
<nowiki>
<nowiki>

Revision as of 19:05, 26 January 2020

Tasks

Init DB

# service postgresql initdb
Hint: the preferred way to do this is now "postgresql-setup initdb"
Initializing database ... OK

Client

The command-line client for PosgreSQL is psql. Connect by running psql as root or as a particular user. Alternatively, specify the username, password, and host:

# psql -U ctxvda -W -h localhost citrix-confdb
## -U for username
## -W to prompt for a password
## -h for hostname
## -p for port
## -d (optional) for the database name

Once in the psql console, commands are issued with a backslash.

Common commands are:

Command Description
\l Lists databases
postgres=# \l
                                    List of databases
     Name      |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
---------------+----------+----------+-------------+-------------+-----------------------
 citrix-confdb | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
 template1     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
               |          |          |             |             | postgres=CTc/postgres
(4 rows)
\c Change database
postgres=# \c citrix-confdb
You are now connected to database "citrix-confdb" as user "postgres".
\dt Describes tables of a database.
citrix-confdb=# \dt
           List of relations
 Schema
\q Quits the console. Same with Ctrl+d
\! Executes a system command. Eg. \! ls
\set Sets parameters in psql. Some parameters are set in .psqlrc.


If the table name is in mixed case, the table name needs to be in quotes.

citrix-confdb=&gt; SELECT * FROM "Value" ;
 ID  |                  Name                   | ValueDWORD |                          ValueSZ                          | ValueMultiSZ | ValueBinary | KeyID
-----+-----------------------------------------+------------+-----------------------------------------------------------+--------------+-------------+-------
   1 | Copyright                               |            | Copyright 2016 Citrix Systems, Inc.  All Rights Reserved. |              |             |    13
   2 | DotVersion                              |         15 |                                                           |              |             |    13
...