当前位置:网站首页>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';
边栏推荐
- How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
- 2022-2028 global physiotherapy clinic industry research and trend analysis report
- Graduation summary
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- 041. (2.10) talk about manpower outsourcing
- ES7 - Optimization of promise
- Getting started with JDBC
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- Usage of laravel conditional array in
- How do microservices aggregate API documents? This wave of operation is too good
猜你喜欢

Win 11 major updates, new features love love.

Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it

Unity webgl optimization

4. Load balancing and dynamic static separation

Administrative division code acquisition

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

2022-2028 global marking ink industry research and trend analysis report

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

Kratos微服务框架下实现CQRS架构模式

Have you learned the correct expression posture of programmers on Valentine's day?
随机推荐
English语法_形容词/副词3级 - 倍数表达
Torch learning notes (3) -- univariate linear regression model (self training)
Computer graduation design PHP sports goods online sales system website
Recommend a simple browser tab
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
What is SQL get connection
Setinterval CPU intensive- Is setInterval CPU intensive?
SQL injection -day16
Reappearance of ASPP (atlas spatial pyramid pooling) code
[combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
TypeScript 官网教程
[combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
2022-2028 global sepsis treatment drug industry research and trend analysis report
CV in transformer learning notes (continuously updated)
Golang string (string) and byte array ([]byte) are converted to each other
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
Administrative division code acquisition
How do microservices aggregate API documents? This wave of operation is too good
198. Looting - Dynamic Planning
Sensor 调试流程