author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 08 Jan 2019 00:32:34 +0200 | |
changeset 2335 | df944b944b30 |
parent 2333 | 3a371f973644 |
child 2337 | c6c55f6a45c8 |
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 |
||
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
70 |
Grant role options |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
71 |
================== |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
72 |
:: |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
73 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
74 |
ALTER USER me WITH SUPERUSER; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
75 |
ALTER USER me WITH CREATEDB CREATEROLE LOGIN; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
76 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
77 |
https://www.postgresql.org/docs/current/sql-createrole.html |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
78 |
``CREATE ROLE``. |
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 |
Change password |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
81 |
=============== |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
82 |
:: |
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 |
ALTER USER me WITH PASSWORD 'PassWord'; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
85 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
86 |
Monitor connections |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
87 |
=================== |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
88 |
:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
89 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
90 |
select * from pg_stat_activity; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
91 |
|
2333
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
92 |
https://www.postgresql.org/docs/current/monitoring-stats.html |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
93 |
The Statistics Collector. |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
94 |
|
2331 | 95 |
Profile queries |
96 |
=============== |
|
97 |
||
98 |
Stats on query execution time:: |
|
99 |
||
100 |
SELECT * FROM pg_stat_statements; |
|
101 |
||
102 |
https://www.postgresql.org/docs/current/pgstatstatements.html |
|
103 |
``pg_stat_statements``. |
|
104 |
https://www.postgresql.org/docs/current/indexes-examine.html |
|
105 |
Examining Index Usage. |
|
106 |
||
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
107 |
List databases, schemas and tables |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
108 |
================================== |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
109 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
110 |
Default database is ``postgres``. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
111 |
|
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
112 |
To list databases and database locales:: |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
113 |
|
1702 | 114 |
$ psql -U pgadmin -l |
115 |
||
116 |
or:: |
|
117 |
||
118 |
=> SELECT datname FROM pg_database WHERE datistemplate = false; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
119 |
=> \l |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
120 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
121 |
To switch databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
122 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
123 |
=> \connect NAME |
1684 | 124 |
|
125 |
Schemas:: |
|
126 |
||
2328 | 127 |
\dn * |
128 |
select schema_name from information_schema.schemata; |
|
129 |
select nspname from pg_catalog.pg_namespace; |
|
1684 | 130 |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
131 |
To list all tables in the current database:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
132 |
|
2328 | 133 |
\dt |
134 |
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
|
135 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
136 |
or in all schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
137 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
138 |
=> \dt *.* |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
139 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
140 |
List views:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
141 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
142 |
\dv |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
143 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
144 |
List users:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
145 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
146 |
\du |
2299 | 147 |
\du+ |
148 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
149 |
List schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
150 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
151 |
\dn |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
152 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
153 |
List indexes:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
154 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
155 |
\di |
2328 | 156 |
select * from pg_indexes; |
157 |
select * from pg_indexes where schemaname = '...'; |
|
158 |
select * from pg_indexes where schemaname = '...' and tablename = '...'; |
|
159 |
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
|
160 |
|
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
161 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
162 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
163 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
164 |
a.attname as column_name |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
165 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
166 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
167 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
168 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
169 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
170 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
171 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
172 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
173 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
174 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
175 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
176 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
177 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
178 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
179 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
180 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
181 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
182 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
183 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
184 |
array_to_string(array_agg(a.attname), ', ') as column_names |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
185 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
186 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
187 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
188 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
189 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
190 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
191 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
192 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
193 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
194 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
195 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
196 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
197 |
group by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
198 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
199 |
i.relname |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
200 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
201 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
202 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
203 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
204 |
List index usage:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
205 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
206 |
SELECT * FROM pg_stat_all_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
207 |
SELECT * FROM pg_stat_all_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
208 |
SELECT * FROM pg_stat_user_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
209 |
SELECT * FROM pg_stat_user_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
210 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
211 |
List constraints:: |
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_constraint; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
214 |
SELECT * FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tblName'); |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
215 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
216 |
https://www.postgresql.org/docs/current/catalog-pg-constraint.html |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
217 |
pg_constraint. |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
218 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
219 |
List functions:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
220 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
221 |
\df |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
222 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
223 |
List triggers:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
224 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
225 |
\dy |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
226 |
|
2299 | 227 |
List misc info:: |
228 |
||
229 |
select current_database(); |
|
230 |
select current_schema(); |
|
231 |
select current_user; |
|
232 |
select session_user; |
|
233 |
select current_date; |
|
234 |
select version(); |
|
235 |
||
236 |
https://www.postgresql.org/docs/current/functions-info.html |
|
237 |
System Information Functions. |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
238 |
|
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
239 |
Set default schema. |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
240 |
=================== |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
241 |
:: |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
242 |
|
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
243 |
set search_path to NAME; |
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
244 |
set schema 'NAME'; |
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
245 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
246 |
Database, table, index size |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
247 |
=========================== |
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
248 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
249 |
Database size:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
250 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
251 |
SELECT pg_database_size('geekdb'); -- in bytes |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
252 |
SELECT pg_size_pretty(pg_database_size('dbname')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
253 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
254 |
List of databases sizes:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
255 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
256 |
\l+ |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
257 |
|
1711 | 258 |
List tables sizes:: |
259 |
||
260 |
\d+ |
|
261 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
262 |
Table total size (with indexes):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
263 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
264 |
SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); |
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 |
Sole table size (without indexes and other):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
267 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
268 |
SELECT pg_size_pretty(pg_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
269 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
270 |
Largest table in the PostgreSQL database:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
271 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
272 |
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
273 |
|
2327
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
274 |
MVCC and transaction id |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
275 |
======================= |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
276 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
277 |
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
|
278 |
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
|
279 |
``xmax`` - last transaction that can see row::: |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
280 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
281 |
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
|
282 |
|
2328 | 283 |
Vacuum |
284 |
====== |
|
285 |
||
286 |
Stats on vacuum oprtations per table:: |
|
287 |
||
288 |
select * from pg_stat_user_tables; |
|
289 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
290 |
Using psql client |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
291 |
================= |
1700 | 292 |
|
293 |
Using password file ``~/.pgpass``:: |
|
294 |
||
295 |
# comment |
|
296 |
hostname:port:database:username:password |
|
297 |
hostname:port:*:username:password |
|
298 |
hostname:*:*:username:password |
|
299 |
||
300 |
Connect by:: |
|
301 |
||
302 |
$ psql -U $USER -h $HOST $SCHEMA |
|
303 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
304 |
How to view execution plan:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
305 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
306 |
EXPLAIN query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
307 |
EXPLAIN ANALYZE query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
308 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
309 |
Collect statistics:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
310 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
311 |
ANALYZE table; |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
312 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
313 |
How to redirect the output of query to a file:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
314 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
315 |
\o output_file |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
316 |
SELECT * FROM pg_class; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
317 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
318 |
Switch database:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
319 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
320 |
\connect DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
321 |
\c DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
322 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
323 |
Load external SQL file:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
324 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
325 |
\i my.sql |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
326 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
327 |
Export data |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
328 |
=========== |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
329 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
330 |
Export from ``psql``:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
331 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
332 |
\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
|
333 |
|
2305 | 334 |
Import data |
335 |
=========== |
|
336 |
||
337 |
Import CSV file:: |
|
338 |
||
339 |
\copy tbl_name from 'my.csv' csv; |
|
340 |
\copy tbl_name from 'my.csv' delimiter ':' csv; |
|
341 |
||
342 |
$ psql -U $USER -h $HOST $DB -c "\\copy tbl_name from 'my.csv' csv" |
|
343 |
||
2326 | 344 |
JDBC driver |
345 |
=========== |
|
346 |
||
347 |
https://jdbc.postgresql.org/about/about.html |
|
348 |
The current version of the driver should be compatible with PostgreSQL 8.2 and higher, and Java 6 |
|
349 |
(JDBC 4.0), Java 7 (JDBC 4.1), Java 8 (JDBC 4.2) and Java 9. |
|
350 |
https://jdbc.postgresql.org/download.html |
|
351 |
Java/JDBC/Postgres compatibility table. |
|
352 |
||
2332 | 353 |
plpgsql |
354 |
======= |
|
355 |
||
356 |
Simple function:: |
|
357 |
||
358 |
CREATE OR REPLACE FUNCTION incx(x INTEGER) |
|
359 |
RETURNS INTEGER STABLE AS $$ |
|
360 |
BEGIN |
|
361 |
RETURN x + 1; |
|
362 |
END; |
|
363 |
$$ LANGUAGE plpgsql; |
|
364 |
||
365 |
Support for procedures is added only in Postgres v11. |
|
366 |
||
367 |
https://www.postgresql.org/docs/current/sql-createfunction.html |
|
368 |
``CREATE FUNCTION``. |
|
369 |
https://www.postgresql.org/docs/current/plpgsql-control-structures.html |
|
370 |
plpgsql control structures. |
|
371 |
https://www.postgresql.org/docs/current/errcodes-appendix.html |
|
372 |
PostgreSQL Error Codes. |