Zde je několik způsobů, jak zobrazit seznam databází při použití psql s PostgreSQL.
První možnost lze použít, když jsme již připojeni k PostgreSQL. Druhou možnost lze použít, když aktuálně nemáme připojení k Postgres.
\l a \list Příkazy
Můžeme použít buď \l nebo \list vrátit seznam databází.
Syntaxe vypadá takto:
\l[+] or \list[+] [ pattern ]
Části v hranatých závorkách [] jsou volitelné.
Takže nejrychlejší/nejjednodušší způsob, jak získat seznam databází, je tento:
\l Příklad výsledku:
List of databases +--------------+----------+----------+-------------+-------------+-----------------------+ | Name | Owner | Encoding | Collate | Ctype | Access privileges | +--------------+----------+----------+-------------+-------------+-----------------------+ | barney | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | | krankykranes | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | | music | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | | pagila | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | | pethotel | barney | 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 | +--------------+----------+----------+-------------+-------------+-----------------------+
Aby bylo jasno, spustil jsem tento příkaz, když jsem již byl připojen k PostgreSQL.
Stejného výsledku lze dosáhnout pomocí \list místo \l .
Můžeme přidat znaménko plus (+ ) zobrazíte další informace o každé tabulce:
\l+ Příklad výsledku:
List of databases +--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+ | Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description | +--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+ | barney | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 8473 kB | pg_default | | | krankykranes | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 8289 kB | pg_default | | | music | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 8225 kB | pg_default | | | pagila | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 16 MB | pg_default | | | pethotel | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 8177 kB | pg_default | | | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 8097 kB | pg_default | default administrative connection database | | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 7905 kB | pg_default | unmodifiable empty database | | | | | | | postgres=CTc/postgres | | | | | template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 7905 kB | pg_default | default template for new databases | | | | | | | postgres=CTc/postgres | | | | +--------------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------+
Získáme tak několik sloupců navíc s informacemi o velikosti, tabulkovém prostoru atd.
Můžeme také použít vzor k vrácení pouze těch databází, které se vzoru shodují:
\l krank* Příklad výsledku:
List of databases +--------------+----------+----------+-------------+-------------+-------------------+ | Name | Owner | Encoding | Collate | Ctype | Access privileges | +--------------+----------+----------+-------------+-------------+-------------------+ | krankykranes | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | +--------------+----------+----------+-------------+-------------+-------------------+
-l a --list Možnosti připojení
Když zrovna nejsme připojeni k Postgresu, můžeme se připojit pomocí -l nebo --list možnost připojení.
Když je zadána jedna z těchto možností, psql se připojí k Postgresu, zobrazí seznam všech dostupných databází a poté skončí.
Lze jej použít následujícími způsoby:
-l
--list Takže namísto použití zpětného lomítka je to buď pomlčka, nebo dvě pomlčky (podle toho, kterou používáte).
Chcete-li použít tuto možnost, otevřete nové okno terminálu nebo příkazový řádek a zadejte následující:
psql -l Za předpokladu, že je to ve vaší proměnné PATH, mělo by to spustit PostgreSQL, vypsat všechny databáze a poté ukončit.
Příklad výsledku:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+----------+----------+-------------+-------------+-----------------------
barney | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
krankykranes | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
music | barney | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
pagila | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
pethotel | barney | 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
Totéž lze použít pomocí --list :
psql --list