当前位置:网站首页>Common PostgreSQL commands
Common PostgreSQL commands
2022-07-03 18:37:00 【North is South】
Common commands
Studying recently PostgreSQL Technical system , The previous articles introduced permissions , journal , Today, learn some common commands .
Common administrative commands
1 View user information
View all users and corresponding permissions in the current database
\du
see schema Information about
\dn
stay PG Inside schema and user There are differences , Users are independent of database ,schema, Out of table ,MySQL in user Basically equal to schema . PG The default login is Specifies the name of the database public schema . have access to \c Switch to the corresponding schema below
\c
schema_nameuser_name
Verify the current schema Which is ?
show search_path ;
2 View database information
You can use it directly \l ( Lowercase letters L) Show all databases to , Character set ,
\l
If we want to switch to another database , mysql Use use dbname; PG Use
\c dbname [username]
3 View information about the table
View the current schema All the watches inside
\d
View table structure
\d+ tablename
See which users have access to the table
\z tablename
test=# \z proctest
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+----------+-------+-------------------+-------------------+----------
public | proctest | table | | |
(1 row)
### Assign to xx User check list proctest Of select jurisdiction .
test=# grant select on proctest to xx;
GRANT
test=# \z proctest
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+----------+-------+-----------------------------+-------------------+----------
public | proctest | table | test_user=arwdDxt/test_user+| |
| | | xx=r/test_user | |
(1 row)
Look at the index
\di index_name
see Parameters
Direct execution show Parameter name is enough .
postgresql=# show log_statement;
log_statement
---------------
all
(1 row)
Other command lines
\x The meaning of the order is :psql The client displays only one field and its value per line . similar mysql Medium \G
\h: see SQL Explanation of the order , such as \h select.
\?: see psql The list of commands .
\e: Open the text editor .
\conninfo: List current database and connection information .
Commonly used sql
View execution plan
postgresql=# explain select * from t1 where id=2;
QUERY PLAN
----------------------------------------------------
Seq Scan on t1 (cost=0.00..41.88 rows=13 width=4)
Filter: (id = 2)
(2 rows)
View session connection information
select * from
pg_stat_activity;
View long running SQL
SELECT datname,
pid,
usename,
query_start,
STATE,
left(query,40) query,
now()-query_start
FROM pg_stat_activity
WHERE STATE<>'idle'
AND (backend_xid IS NOT NULL
OR backend_xmin IS NOT NULL)
ORDER BY now()-query_start;
View the statistics of the table
select relowner::regrole,relname,relkind,relpages,reltuples from pg_class where relname='t1';
Look at the table with high expansion TOP 10, If the expansion is too high, you need to perform , for example vacuum t1,vacuum Can't lock the watch ,vacuum full Lock the watch
SELECT relname AS TABLE_NAME,
pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) AS table_size,
n_dead_tup,
n_live_tup,
(n_dead_tup * 100 / (n_live_tup + n_dead_tup))AS dead_tup_ratio
FROM pg_stat_user_tables
WHERE n_dead_tup<>0 order by 5 desc LIMIT 10;
View the number of database user connections
select datname,usename,state,count(*) from pg_stat_activity group by datname,usename,state order by 1,2,3,4;
The execution time of killing exceeds 10 Minutes of sql
select pg_terminate_backend(pid) from pg_stat_activity where clock_timestamp()-query_start > '10 min' and backend_type='client backend';
Kill more than 10 Minutes long transaction session
select pg_terminate_backend(pid) from pg_stat_activity where clock_timestamp()-xact_start > '10 min' and backend_type='client backend';
边栏推荐
- 圖像24比特深度轉8比特深度
- After nohup NPM start &, close the shell window directly, and the process closes accordingly
- Read the paper glodyne global topology preserving dynamic network embedding
- How to draw non overlapping bubble chart in MATLAB
- What London Silver Trading software supports multiple languages
- Opencv learning notes (continuously updated)
- 2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- Administrative division code acquisition
- ES7 - Optimization of promise
猜你喜欢

Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)

Theoretical description of linear equations and summary of methods for solving linear equations by eigen

Implementation of cqrs architecture mode under Kratos microservice framework

Torch learning notes (3) -- univariate linear regression model (self training)

What problems can cross-border e-commerce sellers solve with multi platform ERP management system

Summary and Reflection on the third week of winter vacation

Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
![[Godot] add menu button](/img/44/ef1e6ac25bcbc8cc3ecc00c52f8ee2.jpg)
[Godot] add menu button

Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)

The vscode code is automatically modified to a compliance code when it is formatted and saved
随机推荐
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
A. Odd Selection【BruteForce】
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
MySQL duplicate check
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
Valentine's day, send you a little red flower~
Database creation, addition, deletion, modification and query
The vscode code is automatically modified to a compliance code when it is formatted and saved
Usage of laravel conditional array in
English grammar_ Adjective / adverb Level 3 - multiple expression
Caddy server agent
SDNUOJ1015
What does foo mean in programming?
How to quickly view the inheritance methods of existing models in torchvision?
Getting started with JDBC
Recommend a simple browser tab
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
189. Rotation array
How about the Moco model?
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing