当前位置:网站首页>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';
边栏推荐
- How does GCN use large convolution instead of small convolution? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
- Mysql45 lecture learning notes (II)
- How to track the real-time trend of Bank of London
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Redis on local access server
- Software development freelancer's Road
- Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
- Unity webgl optimization
- Prototype inheritance..
- MySQL duplicate check
猜你喜欢
Ping problem between virtual machine and development board
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
Torch learning notes (3) -- univariate linear regression model (self training)
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
CTO and programmer were both sentenced for losing control of the crawler
Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
English grammar_ Noun classification
Sensor debugging process
Multifunctional web file manager filestash
随机推荐
How to expand the capacity of golang slice slice
204. Count prime
JS_ Array_ sort
[combinatorics] generating function (positive integer splitting | unordered non repeated splitting example)
Mysql45 lecture learning notes (II)
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
Use of unsafe class
041. (2.10) talk about manpower outsourcing
How many convolution methods does deep learning have? (including drawings)
Coordinate layer conversion tool (video)
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
2022-2028 global lithium battery copper foil industry research and trend analysis report
Typescript official website tutorial
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
Sensor debugging process
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
Raft log replication
Day-27 database