当前位置:网站首页>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_name
user_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';
边栏推荐
- Implementation of cqrs architecture mode under Kratos microservice framework
- CV in transformer learning notes (continuously updated)
- JS_ Array_ sort
- How to draw non overlapping bubble chart in MATLAB
- [combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Real time split network (continuous update)
- What is the function of registering DLLs- What does registering a DLL do?
- Have you learned the correct expression posture of programmers on Valentine's day?
- [combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
猜你喜欢
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
How to quickly view the inheritance methods of existing models in torchvision?
Ping problem between virtual machine and development board
Caddy server agent
Read the paper glodyne global topology preserving dynamic network embedding
4. Load balancing and dynamic static separation
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Torch learning notes (3) -- univariate linear regression model (self training)
Nodejs (01) - introductory tutorial
Shell script return value with which output
随机推荐
Redis on local access server
What is the function of registering DLLs- What does registering a DLL do?
Raft log replication
Multifunctional web file manager filestash
Su embedded training - Day10
Database creation, addition, deletion, modification and query
Sensor 调试流程
Image 24 bit depth to 8 bit depth
How do microservices aggregate API documents? This wave of operation is too good
What kind of experience is it when the Institute earns 20000 yuan a month?
Opencv learning notes (continuously updated)
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
The vscode code is automatically modified to a compliance code when it is formatted and saved
Torch learning notes (5) -- autograd
English grammar_ Noun classification
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
Recent learning experience
[combinatorics] generating function (example of generating function | calculating generating function with given general term formula | calculating general term formula with given generating function)
Software development freelancer's Road
Sepconv (separable revolution) code recurrence