GNU/Linux >> Znalost Linux >  >> Cent OS

Užitečné RHV Engine PostgreSQL databázové dotazy

Tento příspěvek vysvětlí některé praktické příkazy psql a popíše, jak spouštět dotazy PostgreSQL z příkazového řádku, abyste získali užitečné informace z databáze motoru.

Připojte se k databázi ‚Postgres‘ z příkazového řádku

Připojení k databázi „Postgres“:

# su - postgres
Last login: Thu Apr 30 20:25:36 AEST 2020 on pts/1

Protože postgres pochází ze softwarových kolekcí, musíte povolit postgresql pro připojení databáze motoru:

Pro 4.2.8:

-bash-4.2$ scl enable rh-postgresql95 "psql -d engine -U postgres"
psql (9.5.14)
Type "help" for help.
engine=#

Pro 4.3.6:

-bash-4.2$ scl enable rh-postgresql10 "psql -d engine -U postgres"
psql (10.6)
Type "help" for help. 

Některé praktické a užitečné příkazy psql

1. Získejte nápovědu k příkazům psql.

Chcete-li znát všechny dostupné příkazy psql, použijte \? příkaz získat pomoc.

engine=# \?

2. Seznam všech databází s ‚\l‘.

RHV má dvě související databáze:engine a ovirt-engine-history. RHV vytváří PostgreSQL databázi s názvem engine. Při instalaci balíčku ovirt-engine-dwh se vytvoří druhá databáze nazvaná ovirt-engine-history, která obsahuje historické konfigurační informace a statistické metriky shromažďované každou minutu v průběhu času z provozní databáze motoru. Informace o dvou databázích můžete vidět níže:

engine=# \l
List of databases
Name                    | Owner                | Encoding | Collate | Ctype | Access privileges
----------------------+------------------------+----------+-------------+-------------+-----------------------
engine                  | engine               | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
ovirt_engine_history    | ovirt_engine_history | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
postgres                | postgres             | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
templates               | 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
(5 rows)

Pomocí „\l+“ získáte podrobné informace:

engine=# \l+
List of databases
Name                  | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace |  Description
----------------------+----------------------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
engine                | engine | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 28 MB | pg_default |
ovirt_engine_history  | ovirt_engine_history | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 48 MB | pg_default |
postgres              | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 7288 kB | pg_default | default administrative connection database
templates             | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 7153 kB | pg_default | unmodifiable empty database  | | | | | postgres=CTc/postgres | | |
template1             | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +| 7288 kB | pg_default | default template for new databases | | | | | postgres=CTc/postgres | | |
(5 rows)

3. Zobrazte informace o aktuálním připojení k databázi.

engine=# \conninfo
You are connected to database "engine" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

4. Seznam dostupných schémat:

engine=# \dn+
                   List of schemas
Name      | Owner    | Access privileges    | Description
----------+----------+----------------------+------------------------
aaa_jdbc  | engine   |                      |
public    | postgres | postgres=UC/postgres+| standard public schema
          |          | =UC/postgres         |
(2 rows)

5. Seznam všech tabulek s ‚\z‘:

engine=# \z
Access privileges
Schema  | Name                                             | Type | Access privileges | Column privileges | Policies
--------+--------------------------------------------------+----------+-------------------+-------------------+----------
public  | active_migration_network_interfaces              | view | | |
public  | ad_groups                                        | table| | |
...
public  | all_disks                                        | view | | |
public  | all_disks_for_vms                                | view | | |
public  | all_disks_including_snapshots                    | view | | |
public  | all_disks_including_snapshots_and_memory         | view | | |
public  | audit_log                                        | table | | |
public  | cluster                                          | table | | |

6. Zobrazit tabulky v aktuální databázi motoru:

engine-# \dt
List of relations
Schema  | Name                                | Type  | Owner
--------+-------------------------------------+-------+--------
public  | ad_groups                           | table | engine
public  | cluster                             | table | engine
public  | cluster_features                    | table | engine
public  | cluster_policies                    | table | engine
public  | cluster_policy_units                | table | engine
public  | disk_lun_map                        | table | engine
public  | disk_profiles                       | table | engine
public  | disk_vm_element                     | table | engine
public  | dwh_osinfo                          | table | engine
...

public  | vds_static                          | table | engine
public  | vds_statistics                      | table | engine
public  | vfs_config_labels                   | table | engine
public  | vfs_config_networks                 | table | engine
public  | vm_device                           | table | engine

7. Popište tabulku:

engine-# \d table_name

Například:

engine-# \d vds_static
                                Table "public.vds_static"
Column                         | Type                     | Modifiers
-------------------------------+--------------------------+----------------------------------------------------
vds_id                         | uuid                     | not null
vds_name                       | character varying(255)   | not null
vds_unique_id                  | character varying(128)   |
host_name                      | character varying(255)   | not null
...
kernel_cmdline                 | text                     |
last_stored_kernel_cmdline     | text                     |
reinstall_required             | boolean                  | not null default false

Spusťte dotaz SQL z tabulek motoru a získejte informace

Zde je několik příkladů dotazů:

1. Chcete-li získat všechny uživatele v databázi stroje PostgreSQL:

engine=# select user_id, name, username from users;

2. Chcete-li získat celou síť:

engine=# select id,name,description,storage_pool_id,vdsm_name from network;
                   id                 | name      | description        |           storage_pool_id            | vdsm_name
--------------------------------------+-----------+--------------------+--------------------------------------+-----------
              <network id>            | ovirtmgmt | Management Network |          <storage_pool_id>           | ovirtmgmt
              <network id>            | vm_pub    | vm network         |          <storage_pool_id>           | vm_pub

3. Chcete-li získat informace o hostitelích KVM:

engine=# select vds_name, vds_unique_id, port,cluster_id ,_create_date from vds_static;
vds_name                  |          vds_unique_id               | port  |            cluster_id                | _create_date
--------------------------+--------------------------------------+-------+--------------------------------------+-------------------------------
xxx.xxx.xxx.xxx           |                       | 54321 |                          | 
(1 row)

4. Chcete-li zkontrolovat maximální povolený počet klientských připojení:

engine=# select setting::bigint from pg_settings where name='max_connections';
setting
---------
150
(1 row)

5. Chcete-li před zápisem do dočasných souborů na disku zkontrolovat velikost paměti, kterou mají použít interní operace řazení a hašovací tabulky:

engine=# select name, setting, unit, source from pg_settings where name = 'work_mem';
name | setting | unit | source
----------+---------+------+--------------------
work_mem | 8192 | kB | configuration file
(1 row)

6. Chcete-li zkontrolovat sdílené vyrovnávací paměti o tom, kolik paměti je vyhrazeno PostgreSQL pro ukládání dat do mezipaměti:

engine=# select setting::bigint from pg_settings where name='shared_buffers';
setting
---------
16384
(1 row)

7. Chcete-li zkontrolovat odhadované maximální využití paměti RAM:

engine=# select pg_size_pretty(shared_buffers::bigint*block_size + max_connections*work_mem*1024 + autovacuum_max_workers*(case when autovacuum_work_mem=-1 then maintenance_work_mem else autovacuum_work_mem end)*1024) as estimated_max_ram_usage from ( select (select setting::bigint from pg_settings where name='block_size') as block_size, (select setting::bigint from pg_settings where name='shared_buffers') as shared_buffers, (select setting::bigint from pg_settings where name='max_connections') as max_connections,(select setting::bigint from pg_settings where name='work_mem') as work_mem, (select setting::bigint from pg_settings where name='autovacuum_max_workers') as autovacuum_max_workers,(select setting::bigint from pg_settings where name='autovacuum_work_mem') as autovacuum_work_mem,(select setting::bigint from pg_settings where name='maintenance_work_mem') as maintenance_work_mem)as _ ;
estimated_max_ram_usage
-------------------------
1712 MB
(1 row)

Ostatní

1. Znovu načtěte konfigurační soubory pomocí select pg_reload_config():

engine=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)

2. Zobrazte historii příkazů, použijte příkaz \s.

engine=# \s

3. Chcete-li ukončit psql, použijte příkaz \q a stiskněte Enter pro ukončení psql.

engine=# \q


Cent OS
  1. Jak nainstalovat rozšíření databáze PostGIS PostgreSQL na CentOS 8

  2. Jak nainstalovat PostgreSQL na CentOS 7 / RHEL 7

  3. Jak změnit heslo databáze motoru pro RHVM Engine

  1. Databázový server PostgreSQL

  2. RedHat Virtualization (RHV) – Možnosti konfigurace motoru

  3. Pochopení živého sloučení ve virtualizaci RedHat (RHV)

  1. PostgreSQL Drop databáze s příklady

  2. Jak vytvořit databázi v PostgreSQL

  3. Jak nainstalovat PostgreSQL na Ubuntu 20.04