author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 29 May 2019 23:17:25 +0300 | |
changeset 2370 | a3abad65bf9f |
parent 2343 | 984ccb104a6e |
child 2373 | 07d81edeaba6 |
permissions | -rw-r--r-- |
1683 | 1 |
.. -*- coding: utf-8; -*- |
2 |
||
3 |
========== |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
4 |
Postgres |
1683 | 5 |
========== |
6 |
.. contents:: |
|
1905
fba288d59662
Include only local subsections into TOC. This prevent duplication of
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1711
diff
changeset
|
7 |
:local: |
1683 | 8 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
9 |
Installing on Debian |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
10 |
==================== |
1683 | 11 |
|
12 |
Install and create new user and database:: |
|
13 |
||
14 |
$ sudo apt-get install postgresql postgresql-client |
|
15 |
$ sudo su - postgres |
|
1708
f1924a82b246
Update instruction.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1703
diff
changeset
|
16 |
% psql |
2305 | 17 |
postgres=# CREATE USER myuser WITH PASSWORD 'mypass'; |
18 |
postgres=# CREATE DATABASE mydb OWNER myuser; |
|
1683 | 19 |
postgres=# \q |
20 |
||
21 |
Connect as user ``mypguser`` to new database:: |
|
22 |
||
23 |
$ su - mypguser |
|
24 |
$ psql mypgdatabase |
|
25 |
||
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
26 |
In order to create local host superuser:: |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
27 |
|
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
28 |
$ sudo su - postgres |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
29 |
$ createuser --superuser USER |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
30 |
$ exit |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
31 |
$ sudo -u USER psql |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
32 |
|
1683 | 33 |
.. |
34 |
||
2228
837f1337c59b
Removed indentation that compiled into <blockquote>.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1912
diff
changeset
|
35 |
https://wiki.debian.org/PostgreSql |
837f1337c59b
Removed indentation that compiled into <blockquote>.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1912
diff
changeset
|
36 |
Debian wiki instructions. |
1683 | 37 |
|
2319 | 38 |
Check version |
39 |
============= |
|
40 |
||
41 |
By SQL query:: |
|
42 |
||
43 |
SELECT version(); |
|
44 |
SHOW server_version; |
|
45 |
SHOW server_version_num; |
|
46 |
||
47 |
From executable:: |
|
48 |
||
49 |
$ postgres -V |
|
50 |
||
2303 | 51 |
Check locations |
52 |
=============== |
|
53 |
:: |
|
54 |
||
55 |
SHOW hba_file; |
|
56 |
SHOW config_file; |
|
57 |
||
2326 | 58 |
Check settings |
59 |
============== |
|
60 |
:: |
|
61 |
||
62 |
SELECT name, setting FROM pg_settings; |
|
63 |
||
2328 | 64 |
Check extensions |
65 |
================ |
|
66 |
:: |
|
67 |
||
68 |
SELECT * FROM pg_available_extensions; |
|
69 |
||
2338 | 70 |
Limitations |
71 |
=========== |
|
72 |
||
73 |
https://wiki.postgresql.org/wiki/TOAST |
|
74 |
TOAST is a mechanism PostgreSQL uses to keep physical data rows from exceeding the size of a data |
|
75 |
block (typically 8KB). |
|
76 |
||
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
77 |
Grant role options |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
78 |
================== |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
79 |
:: |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
80 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
81 |
ALTER USER me WITH SUPERUSER; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
82 |
ALTER USER me WITH CREATEDB CREATEROLE LOGIN; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
83 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
84 |
https://www.postgresql.org/docs/current/sql-createrole.html |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
85 |
``CREATE ROLE``. |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
86 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
87 |
Change password |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
88 |
=============== |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
89 |
:: |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
90 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
91 |
ALTER USER me WITH PASSWORD 'PassWord'; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
92 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
93 |
Monitor connections |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
94 |
=================== |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
95 |
:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
96 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
97 |
select * from pg_stat_activity; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
98 |
|
2333
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
99 |
https://www.postgresql.org/docs/current/monitoring-stats.html |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
100 |
The Statistics Collector. |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
101 |
|
2331 | 102 |
Profile queries |
103 |
=============== |
|
104 |
||
105 |
Stats on query execution time:: |
|
106 |
||
107 |
SELECT * FROM pg_stat_statements; |
|
108 |
||
109 |
https://www.postgresql.org/docs/current/pgstatstatements.html |
|
110 |
``pg_stat_statements``. |
|
111 |
https://www.postgresql.org/docs/current/indexes-examine.html |
|
112 |
Examining Index Usage. |
|
113 |
||
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
114 |
List databases, schemas and tables |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
115 |
================================== |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
116 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
117 |
Default database is ``postgres``. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
118 |
|
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
119 |
To list databases and database locales:: |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
120 |
|
1702 | 121 |
$ psql -U pgadmin -l |
122 |
||
123 |
or:: |
|
124 |
||
125 |
=> SELECT datname FROM pg_database WHERE datistemplate = false; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
126 |
=> \l |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
127 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
128 |
To switch databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
129 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
130 |
=> \connect NAME |
1684 | 131 |
|
132 |
Schemas:: |
|
133 |
||
2328 | 134 |
\dn * |
135 |
select schema_name from information_schema.schemata; |
|
136 |
select nspname from pg_catalog.pg_namespace; |
|
1684 | 137 |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
138 |
To list all tables in the current database:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
139 |
|
2328 | 140 |
\dt |
141 |
SELECT table_schema, table_name FROM information_schema.tables ORDER BY table_schema, table_name; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
142 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
143 |
or in all schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
144 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
145 |
=> \dt *.* |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
146 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
147 |
List views:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
148 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
149 |
\dv |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
150 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
151 |
List users:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
152 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
153 |
\du |
2299 | 154 |
\du+ |
155 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
156 |
List schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
157 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
158 |
\dn |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
159 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
160 |
List indexes:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
161 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
162 |
\di |
2328 | 163 |
select * from pg_indexes; |
164 |
select * from pg_indexes where schemaname = '...'; |
|
165 |
select * from pg_indexes where schemaname = '...' and tablename = '...'; |
|
166 |
select * from pg_indexes where schemaname = '...' and indexname = '...'; |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
167 |
|
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
168 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
169 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
170 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
171 |
a.attname as column_name |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
172 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
173 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
174 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
175 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
176 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
177 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
178 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
179 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
180 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
181 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
182 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
183 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
184 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
185 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
186 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
187 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
188 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
189 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
190 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
191 |
array_to_string(array_agg(a.attname), ', ') as column_names |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
192 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
193 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
194 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
195 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
196 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
197 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
198 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
199 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
200 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
201 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
202 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
203 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
204 |
group by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
205 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
206 |
i.relname |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
207 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
208 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
209 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
210 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
211 |
List index usage:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
212 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
213 |
SELECT * FROM pg_stat_all_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
214 |
SELECT * FROM pg_stat_all_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
215 |
SELECT * FROM pg_stat_user_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
216 |
SELECT * FROM pg_stat_user_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
217 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
218 |
List constraints:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
219 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
220 |
SELECT * FROM pg_constraint; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
221 |
SELECT * FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tblName'); |
2337 | 222 |
select * from pg_constraint where conname like '%conName%'; |
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
223 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
224 |
https://www.postgresql.org/docs/current/catalog-pg-constraint.html |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
225 |
pg_constraint. |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
226 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
227 |
List functions:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
228 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
229 |
\df |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
230 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
231 |
List triggers:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
232 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
233 |
\dy |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
234 |
|
2299 | 235 |
List misc info:: |
236 |
||
237 |
select current_database(); |
|
238 |
select current_schema(); |
|
239 |
select current_user; |
|
240 |
select session_user; |
|
241 |
select current_date; |
|
242 |
select version(); |
|
243 |
||
244 |
https://www.postgresql.org/docs/current/functions-info.html |
|
245 |
System Information Functions. |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
246 |
|
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
247 |
Set default schema. |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
248 |
=================== |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
249 |
:: |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
250 |
|
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
251 |
set search_path to NAME; |
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
252 |
set schema 'NAME'; |
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
253 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
254 |
Database, table, index size |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
255 |
=========================== |
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
256 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
257 |
Database size:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
258 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
259 |
SELECT pg_database_size('geekdb'); -- in bytes |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
260 |
SELECT pg_size_pretty(pg_database_size('dbname')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
261 |
|
2338 | 262 |
SELECT datname, pg_database_size(datname) FROM pg_database; |
263 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
264 |
List of databases sizes:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
265 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
266 |
\l+ |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
267 |
|
1711 | 268 |
List tables sizes:: |
269 |
||
270 |
\d+ |
|
271 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
272 |
Table total size (with indexes):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
273 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
274 |
SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
275 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
276 |
Sole table size (without indexes and other):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
277 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
278 |
SELECT pg_size_pretty(pg_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
279 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
280 |
Largest table in the PostgreSQL database:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
281 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
282 |
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; |
2339
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
283 |
SELECT relname, pg_size_pretty(pg_relation_size(p.oid)) FROM pg_class ORDER BY relpages DESC; |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
284 |
|
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
285 |
Largest tables with TOAST:: |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
286 |
|
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
287 |
SELECT p.relname, t.relname, pg_size_pretty(pg_relation_size(p.oid)), p.relpages |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
288 |
FROM pg_class p |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
289 |
LEFT JOIN pg_class t ON t.reltoastrelid = p.oid |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
290 |
ORDER BY p.relpages DESC; |
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
291 |
|
2343 | 292 |
Size of types:: |
293 |
||
294 |
SELECT pg_column_size(5::smallint); |
|
295 |
SELECT pg_column_size(5::int); |
|
296 |
SELECT pg_column_size(5::bigint); |
|
297 |
||
298 |
https://wiki.postgresql.org/wiki/Disk_Usage |
|
299 |
Finding the size of various object in your database. |
|
300 |
||
2327
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
301 |
MVCC and transaction id |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
302 |
======================= |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
303 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
304 |
As being MVCC Postgres doesn't delete or update existing rows. Row visibility is controled by |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
305 |
transaction id (monotonous incremented number): ``xmin`` - first transaction that can see row, |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
306 |
``xmax`` - last transaction that can see row::: |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
307 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
308 |
SELECT xmin, xmax, * FROM foo WHERE id < 5 ORDER BY id; |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
309 |
|
2328 | 310 |
Vacuum |
311 |
====== |
|
312 |
||
313 |
Stats on vacuum oprtations per table:: |
|
314 |
||
315 |
select * from pg_stat_user_tables; |
|
316 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
317 |
Using psql client |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
318 |
================= |
1700 | 319 |
|
320 |
Using password file ``~/.pgpass``:: |
|
321 |
||
322 |
# comment |
|
323 |
hostname:port:database:username:password |
|
324 |
hostname:port:*:username:password |
|
325 |
hostname:*:*:username:password |
|
326 |
||
327 |
Connect by:: |
|
328 |
||
329 |
$ psql -U $USER -h $HOST $SCHEMA |
|
330 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
331 |
How to view execution plan:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
332 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
333 |
EXPLAIN query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
334 |
EXPLAIN ANALYZE query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
335 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
336 |
Collect statistics:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
337 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
338 |
ANALYZE table; |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
339 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
340 |
How to redirect the output of query to a file:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
341 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
342 |
\o output_file |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
343 |
SELECT * FROM pg_class; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
344 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
345 |
Switch database:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
346 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
347 |
\connect DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
348 |
\c DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
349 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
350 |
Load external SQL file:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
351 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
352 |
\i my.sql |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
353 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
354 |
Export data |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
355 |
=========== |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
356 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
357 |
Export from ``psql``:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
358 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
359 |
\copy (SELECT * FROM foo) TO '/tmp/my.csv' WITH CSV |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
360 |
|
2305 | 361 |
Import data |
362 |
=========== |
|
363 |
||
364 |
Import CSV file:: |
|
365 |
||
366 |
\copy tbl_name from 'my.csv' csv; |
|
367 |
\copy tbl_name from 'my.csv' delimiter ':' csv; |
|
368 |
||
369 |
$ psql -U $USER -h $HOST $DB -c "\\copy tbl_name from 'my.csv' csv" |
|
370 |
||
2326 | 371 |
JDBC driver |
372 |
=========== |
|
373 |
||
374 |
https://jdbc.postgresql.org/about/about.html |
|
375 |
The current version of the driver should be compatible with PostgreSQL 8.2 and higher, and Java 6 |
|
376 |
(JDBC 4.0), Java 7 (JDBC 4.1), Java 8 (JDBC 4.2) and Java 9. |
|
377 |
https://jdbc.postgresql.org/download.html |
|
378 |
Java/JDBC/Postgres compatibility table. |
|
379 |
||
2332 | 380 |
plpgsql |
381 |
======= |
|
382 |
||
383 |
Simple function:: |
|
384 |
||
385 |
CREATE OR REPLACE FUNCTION incx(x INTEGER) |
|
386 |
RETURNS INTEGER STABLE AS $$ |
|
387 |
BEGIN |
|
388 |
RETURN x + 1; |
|
389 |
END; |
|
390 |
$$ LANGUAGE plpgsql; |
|
391 |
||
392 |
Support for procedures is added only in Postgres v11. |
|
393 |
||
394 |
https://www.postgresql.org/docs/current/sql-createfunction.html |
|
395 |
``CREATE FUNCTION``. |
|
396 |
https://www.postgresql.org/docs/current/plpgsql-control-structures.html |
|
397 |
plpgsql control structures. |
|
398 |
https://www.postgresql.org/docs/current/errcodes-appendix.html |
|
399 |
PostgreSQL Error Codes. |