author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sun, 15 Nov 2020 16:55:51 +0200 | |
changeset 2458 | 28c248a41a85 |
parent 2436 | 41f79c1242b2 |
child 2479 | ab3f4aad1b37 |
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; |
|
2388 | 63 |
SELECT name, current_setting(name), source FROM pg_settings; |
2326 | 64 |
|
2328 | 65 |
Check extensions |
66 |
================ |
|
67 |
:: |
|
68 |
||
69 |
SELECT * FROM pg_available_extensions; |
|
70 |
||
2392
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
71 |
Set application name |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
72 |
==================== |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
73 |
|
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
74 |
``application_name`` is displayed in ``pg_stat_activity.application_name``. |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
75 |
|
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
76 |
It can help identify clients of DB. To alter it in current session:: |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
77 |
|
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
78 |
SET application_name = 'some name'; |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
79 |
|
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
80 |
To show current value:: |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
81 |
|
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
82 |
SHOW application_name; |
eddb9a8153b5
Set application name.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2391
diff
changeset
|
83 |
|
2338 | 84 |
Limitations |
85 |
=========== |
|
86 |
||
87 |
https://wiki.postgresql.org/wiki/TOAST |
|
88 |
TOAST is a mechanism PostgreSQL uses to keep physical data rows from exceeding the size of a data |
|
89 |
block (typically 8KB). |
|
90 |
||
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
91 |
List privileges |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
92 |
=============== |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
93 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
94 |
List databases with privileges:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
95 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
96 |
\l |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
97 |
|
2377 | 98 |
List schemas with privileges:: |
99 |
||
100 |
\dn+ |
|
101 |
||
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
102 |
List users with privileges:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
103 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
104 |
\du |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
105 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
106 |
List privileges to objects (tables, views, sequences):: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
107 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
108 |
\dp |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
109 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
110 |
SELECT * FROM information_schema.role_table_grants |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
111 |
WHERE table_name = 'table_name'; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
112 |
|
2379
a38f029fcf84
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2378
diff
changeset
|
113 |
List default privileges:: |
a38f029fcf84
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2378
diff
changeset
|
114 |
|
a38f029fcf84
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2378
diff
changeset
|
115 |
\ddp |
a38f029fcf84
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2378
diff
changeset
|
116 |
|
2380
6effef47c861
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2379
diff
changeset
|
117 |
SELECT * FROM pg_default_acl; |
6effef47c861
List default privileges.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2379
diff
changeset
|
118 |
|
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
119 |
Grant role options |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
120 |
================== |
2385
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
121 |
|
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
122 |
Show role options and group membership:: |
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
123 |
|
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
124 |
\du |
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
125 |
|
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
126 |
Grant role options:: |
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
127 |
|
2385
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
128 |
ALTER USER me WITH SUPERUSER; |
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
129 |
ALTER USER me WITH CREATEDB CREATEROLE LOGIN; |
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
130 |
|
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
131 |
Revoke role options:: |
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
132 |
|
0c07e8df092f
Revoke role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2383
diff
changeset
|
133 |
ALTER USER me WITH NOSUPERUSER; |
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
134 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
135 |
https://www.postgresql.org/docs/current/sql-createrole.html |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
136 |
``CREATE ROLE``. |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
137 |
|
2373 | 138 |
Granting selects:: |
139 |
||
2375 | 140 |
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ro_user; |
141 |
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO rw_user; |
|
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
142 |
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO rw_user; |
2373 | 143 |
|
2374
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
144 |
Making read-only role:: |
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
145 |
|
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
146 |
GRANT CONNECT ON DATABASE mydb TO ro; |
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
147 |
GRANT USAGE ON SCHEMA myschema TO ro; |
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
148 |
GRANT SELECT ON TABLE myschema.myt1, myschema.myt2 TO ro; |
e050994e0810
Making read-only role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2373
diff
changeset
|
149 |
|
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
150 |
Granting default privileges for new objects:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
151 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
152 |
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO rw_user; |
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
153 |
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT ALL ON SEQUENCES TO rw_user; |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
154 |
ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT EXECUTE ON FUNCTIONS TO rw_user; |
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
155 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
156 |
Revoke privileges and drop role |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
157 |
=============================== |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
158 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
159 |
List roles:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
160 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
161 |
SELECT * FROM pg_roles; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
162 |
|
2378 | 163 |
SELECT oid, rolname FROM pg_roles |
164 |
WHERE pg_has_role('xuser', oid, 'member'); |
|
165 |
||
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
166 |
Revoke privileges:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
167 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
168 |
REVOKE ALL PRIVILEGES ON DATABASE mydb FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
169 |
REVOKE ALL PRIVILEGES ON SCHEMA myschem FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
170 |
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA myschem FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
171 |
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA myschem FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
172 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
173 |
do $$ |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
174 |
DECLARE r record; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
175 |
begin |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
176 |
for r in select * from pg_views where schemaname = 'myschem' |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
177 |
loop |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
178 |
execute 'revoke all on ' || quote_ident(r.schemaname) ||'.'|| quote_ident(r.viewname) || ' from "xuser"'; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
179 |
end loop; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
180 |
end $$; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
181 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
182 |
To forbid any operations (read/write) without dealing with individual privileges (any pending user |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
183 |
transactions will fail!):: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
184 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
185 |
REVOKE USAGE ON SCHEMA myschem FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
186 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
187 |
To forbid connection remove privilege from ``public`` role as it inherited by all users and then from user:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
188 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
189 |
REVOKE CONNECT ON DATABASE mydb FROM public; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
190 |
REVOKE CONNECT ON DATABASE mydb FROM xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
191 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
192 |
Role can't be dropped until it owns objects, need to reassign owner:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
193 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
194 |
REASSIGN OWNED BY xuser TO myuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
195 |
|
2377 | 196 |
Some objects may left behind role (like default privileges), they can be dropped via:: |
197 |
||
198 |
DROP OWNED BY xuser; |
|
199 |
||
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
200 |
Remove role:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
201 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
202 |
DROP ROLE xuser; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
203 |
|
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
204 |
Change password |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
205 |
=============== |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
206 |
:: |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
207 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
208 |
ALTER USER me WITH PASSWORD 'PassWord'; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
209 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
210 |
Monitor connections |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
211 |
=================== |
2378 | 212 |
|
213 |
Current user:: |
|
214 |
||
215 |
SELECT current_user; |
|
216 |
SELECT session_user; |
|
217 |
||
2391
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
218 |
Active sessions and queries:: |
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
219 |
|
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
220 |
select * from pg_stat_activity; |
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
221 |
|
2391
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
222 |
select * from pg_stat_activity |
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
223 |
order by usename; |
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
224 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
225 |
|
2333
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
226 |
https://www.postgresql.org/docs/current/monitoring-stats.html |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
227 |
The Statistics Collector. |
3a371f973644
The Statistics Collector.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2332
diff
changeset
|
228 |
|
2331 | 229 |
Profile queries |
230 |
=============== |
|
231 |
||
232 |
Stats on query execution time:: |
|
233 |
||
234 |
SELECT * FROM pg_stat_statements; |
|
235 |
||
2393 | 236 |
SELECT * FROM pg_stat_statements |
237 |
ORDER BY calls DESC; |
|
238 |
||
239 |
SELECT * FROM pg_stat_statements |
|
240 |
WHERE query LIKE '%some%' |
|
241 |
ORDER BY calls DESC; |
|
242 |
||
2399
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
243 |
https://www.postgresql.org/docs/current/pgstatstatements.html |
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
244 |
``pg_stat_statements``. |
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
245 |
https://www.postgresql.org/docs/current/indexes-examine.html |
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
246 |
Examining Index Usage. |
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
247 |
|
2398
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
248 |
Reveal execution details:: |
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
249 |
|
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
250 |
EXPLAIN ANALYSE SELECT 1; |
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
251 |
EXPLAIN (TIMING false, COSTS false, ANALYSE) SELECT 1; |
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
252 |
EXPLAIN (TIMING false, COSTS false, BUFFERS false, VERBOSE false, ANALYSE) SELECT 1; |
ac2b5bc2f2c1
Reveal execution details.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2393
diff
changeset
|
253 |
|
2399
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
254 |
https://www.postgresql.org/docs/11/sql-explain.html |
ed213cf36a1c
Added link to docs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2398
diff
changeset
|
255 |
``EXPLAIN``. |
2331 | 256 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
257 |
List databases, schemas and tables |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
258 |
================================== |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
259 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
260 |
Default database is ``postgres``. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
261 |
|
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
262 |
To list databases and database locales:: |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
263 |
|
1702 | 264 |
$ psql -U pgadmin -l |
265 |
||
266 |
or:: |
|
267 |
||
268 |
=> SELECT datname FROM pg_database WHERE datistemplate = false; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
269 |
=> \l |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
270 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
271 |
To switch databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
272 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
273 |
=> \connect NAME |
1684 | 274 |
|
275 |
Schemas:: |
|
276 |
||
2328 | 277 |
\dn * |
278 |
select schema_name from information_schema.schemata; |
|
279 |
select nspname from pg_catalog.pg_namespace; |
|
1684 | 280 |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
281 |
To list all tables in the current database:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
282 |
|
2328 | 283 |
\dt |
284 |
SELECT table_schema, table_name FROM information_schema.tables ORDER BY table_schema, table_name; |
|
2391
aedbd074ec54
Added extra examples of queries.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2388
diff
changeset
|
285 |
SELECT * FROM pg_class; |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
286 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
287 |
or in all schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
288 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
289 |
=> \dt *.* |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
290 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
291 |
List views:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
292 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
293 |
\dv |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
294 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
295 |
List users:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
296 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
297 |
\du |
2299 | 298 |
\du+ |
299 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
300 |
List schemas:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
301 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
302 |
\dn |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
303 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
304 |
List indexes:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
305 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
306 |
\di |
2328 | 307 |
select * from pg_indexes; |
308 |
select * from pg_indexes where schemaname = '...'; |
|
309 |
select * from pg_indexes where schemaname = '...' and tablename = '...'; |
|
310 |
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
|
311 |
|
2329
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
312 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
313 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
314 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
315 |
a.attname as column_name |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
316 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
317 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
318 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
319 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
320 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
321 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
322 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
323 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
324 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
325 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
326 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
327 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
328 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
329 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
330 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
331 |
|
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
332 |
select |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
333 |
t.relname as table_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
334 |
i.relname as index_name, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
335 |
array_to_string(array_agg(a.attname), ', ') as column_names |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
336 |
from |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
337 |
pg_class t, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
338 |
pg_class i, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
339 |
pg_index ix, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
340 |
pg_attribute a |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
341 |
where |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
342 |
t.oid = ix.indrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
343 |
and i.oid = ix.indexrelid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
344 |
and a.attrelid = t.oid |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
345 |
and a.attnum = ANY(ix.indkey) |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
346 |
and t.relkind = 'r' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
347 |
and t.relname like 'test%' |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
348 |
group by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
349 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
350 |
i.relname |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
351 |
order by |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
352 |
t.relname, |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
353 |
i.relname; |
738316fb865f
Grant role options.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2328
diff
changeset
|
354 |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
355 |
List index usage:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
356 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
357 |
SELECT * FROM pg_stat_all_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
358 |
SELECT * FROM pg_stat_all_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
359 |
SELECT * FROM pg_stat_user_indexes; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
360 |
SELECT * FROM pg_stat_user_tables; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
361 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
362 |
List constraints:: |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
363 |
|
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
364 |
SELECT * FROM pg_constraint; |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
365 |
SELECT * FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tblName'); |
2337 | 366 |
select * from pg_constraint where conname like '%conName%'; |
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
367 |
|
2373 | 368 |
Show constraints and columns to given table:: |
369 |
||
370 |
select |
|
371 |
c.constraint_name, |
|
372 |
x.table_schema as schema_name, x.table_name, x.column_name, |
|
373 |
y.table_schema as foreign_schema_name, y.table_name as foreign_table_name, y.column_name as foreign_column_name |
|
374 |
from information_schema.referential_constraints c |
|
375 |
join information_schema.key_column_usage x |
|
376 |
on x.constraint_name = c.constraint_name |
|
377 |
join information_schema.key_column_usage y |
|
378 |
on y.ordinal_position = x.position_in_unique_constraint |
|
379 |
and y.constraint_name = c.unique_constraint_name |
|
380 |
where y.table_name = 'sa_pnr' |
|
381 |
order by c.constraint_name, x.ordinal_position |
|
382 |
||
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
383 |
https://www.postgresql.org/docs/current/catalog-pg-constraint.html |
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
384 |
pg_constraint. |
2373 | 385 |
https://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys |
386 |
List table foreign keys. |
|
2330
03d20db65d79
List indexes/constraints.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2329
diff
changeset
|
387 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
388 |
List functions:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
389 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
390 |
\df |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
391 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
392 |
List triggers:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
393 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
394 |
\dy |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
395 |
|
2299 | 396 |
List misc info:: |
397 |
||
398 |
select current_database(); |
|
399 |
select current_schema(); |
|
400 |
select current_user; |
|
401 |
select session_user; |
|
402 |
select current_date; |
|
403 |
select version(); |
|
404 |
||
405 |
https://www.postgresql.org/docs/current/functions-info.html |
|
406 |
System Information Functions. |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
407 |
|
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
408 |
Set default schema. |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
409 |
=================== |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
410 |
:: |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
411 |
|
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
412 |
set search_path to NAME; |
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
413 |
set schema 'NAME'; |
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
414 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
415 |
Database, table, index size |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
416 |
=========================== |
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
417 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
418 |
Database size:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
419 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
420 |
SELECT pg_database_size('geekdb'); -- in bytes |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
421 |
SELECT pg_size_pretty(pg_database_size('dbname')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
422 |
|
2338 | 423 |
SELECT datname, pg_database_size(datname) FROM pg_database; |
424 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
425 |
List of databases sizes:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
426 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
427 |
\l+ |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
428 |
|
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
429 |
SELECT pg_database_size('mydb'); |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
430 |
|
2373 | 431 |
List tables sizes (and constraints):: |
1711 | 432 |
|
433 |
\d+ |
|
434 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
435 |
Table total size (with indexes):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
436 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
437 |
SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
438 |
|
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
439 |
Table size with TOAST (without indexes):: |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
440 |
|
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
441 |
SELECT pg_table_size('schemaname.tablename'); |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
442 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
443 |
Sole table size (without indexes and other):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
444 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
445 |
SELECT pg_size_pretty(pg_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
446 |
|
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
447 |
SELECT pg_relation_size(t.schemaname ||'.'|| t.tablename), t.* FROM pg_tables t; |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
448 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
449 |
Largest table in the PostgreSQL database:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
450 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
451 |
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
|
452 |
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
|
453 |
|
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
454 |
Largest tables with TOAST:: |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
455 |
|
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
456 |
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
|
457 |
FROM pg_class p |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
458 |
LEFT JOIN pg_class t ON t.reltoastrelid = p.oid |
cb96d35c46cf
Largest tables with TOAST.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2338
diff
changeset
|
459 |
ORDER BY p.relpages DESC; |
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
460 |
|
2373 | 461 |
Largest TOAST:: |
462 |
||
463 |
select tbl.relname, toast.relname, toast.relpages |
|
464 |
from pg_class tbl, pg_class toast |
|
465 |
where tbl.reltoastrelid = toast.oid |
|
466 |
order by toast.relpages desc; |
|
467 |
||
2383 | 468 |
Index size:: |
469 |
||
470 |
SELECT pg_indexes_size('my_ix'); |
|
471 |
||
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
472 |
Largest index:: |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
473 |
|
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
474 |
select pg_relation_size(oid), * from pg_class |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
475 |
where relkind = 'i' |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
476 |
order by pg_relation_size(oid) desc; |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
477 |
|
2382 | 478 |
select pg_relation_size(i.oid), t.relname as tname, i.relname as iname, * |
479 |
from pg_class i |
|
480 |
join pg_index ix on ix.indexrelid = i.oid |
|
481 |
join pg_class t on t.oid = ix.indrelid |
|
482 |
where i.relkind = 'i' |
|
483 |
order by pg_relation_size(i.oid) desc; |
|
484 |
||
2436 | 485 |
Size of types:: |
486 |
||
487 |
SELECT pg_column_size(5::smallint); |
|
488 |
SELECT pg_column_size(5::int); |
|
489 |
SELECT pg_column_size(5::bigint); |
|
490 |
||
491 |
Size of column:: |
|
492 |
||
493 |
SELECT sum(pg_column_size(col)) FROM tbl; |
|
494 |
||
495 |
Total of all ``pg_column_size`` is less then ``pg_relation_size`` because table is using pages to |
|
496 |
store rows and we might have gaps inside pages. |
|
497 |
||
2381
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
498 |
https://www.postgresql.org/docs/9.6/functions-admin.html |
b26d26c54b05
Largest index. Table size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2380
diff
changeset
|
499 |
Database Object Management Functions. |
2343 | 500 |
https://wiki.postgresql.org/wiki/Disk_Usage |
2382 | 501 |
Finding the size of various object in your database. |
502 |
https://wiki.postgresql.org/wiki/Index_Maintenance |
|
503 |
Index size/usage statistics. |
|
2343 | 504 |
|
2327
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
505 |
MVCC and transaction id |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
506 |
======================= |
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
507 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
508 |
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
|
509 |
transaction id (monotonous incremented number): ``xmin`` - first transaction that can see row, |
2386 | 510 |
``xmax`` - last transaction that can see row:: |
2327
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
511 |
|
24ffd84a171c
MVCC and transaction id.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2326
diff
changeset
|
512 |
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
|
513 |
|
2328 | 514 |
Vacuum |
515 |
====== |
|
516 |
||
517 |
Stats on vacuum oprtations per table:: |
|
518 |
||
519 |
select * from pg_stat_user_tables; |
|
520 |
||
2373 | 521 |
Table statistics |
522 |
================ |
|
523 |
||
524 |
Full table statistics is in ``pg_statistics`` table which is accessible only by superuser. |
|
525 |
||
526 |
Human readable stats are in ``pg_stats`` view. |
|
527 |
||
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
528 |
Using psql client |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
529 |
================= |
1700 | 530 |
|
531 |
Using password file ``~/.pgpass``:: |
|
532 |
||
533 |
# comment |
|
534 |
hostname:port:database:username:password |
|
535 |
hostname:port:*:username:password |
|
536 |
hostname:*:*:username:password |
|
537 |
||
538 |
Connect by:: |
|
539 |
||
2373 | 540 |
$ psql -U $USER -h $HOST -p $PORT $SCHEMA |
1700 | 541 |
|
2388 | 542 |
Switch to long lines format:: |
543 |
||
544 |
\x |
|
545 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
546 |
How to view execution plan:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
547 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
548 |
EXPLAIN query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
549 |
EXPLAIN ANALYZE query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
550 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
551 |
Collect statistics:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
552 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
553 |
ANALYZE table; |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
554 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
555 |
How to redirect the output of query to a file:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
556 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
557 |
\o output_file |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
558 |
SELECT * FROM pg_class; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
559 |
|
2298
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
560 |
Switch database:: |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
561 |
|
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
562 |
\connect DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
563 |
\c DBNAME |
34ffcffdc8df
List users. Switch database.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2297
diff
changeset
|
564 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
565 |
Load external SQL file:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
566 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
567 |
\i my.sql |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
568 |
|
2387
eede82d48288
Execute external file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2386
diff
changeset
|
569 |
Execute external file:: |
eede82d48288
Execute external file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2386
diff
changeset
|
570 |
|
eede82d48288
Execute external file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2386
diff
changeset
|
571 |
psql -f my.sql |
eede82d48288
Execute external file.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2386
diff
changeset
|
572 |
|
2300
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
573 |
Export data |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
574 |
=========== |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
575 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
576 |
Export from ``psql``:: |
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
577 |
|
e538e158c38b
Export data. Load external SQL file. List schemas/indexes/etc.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2299
diff
changeset
|
578 |
\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
|
579 |
|
2305 | 580 |
Import data |
581 |
=========== |
|
582 |
||
583 |
Import CSV file:: |
|
584 |
||
585 |
\copy tbl_name from 'my.csv' csv; |
|
586 |
\copy tbl_name from 'my.csv' delimiter ':' csv; |
|
587 |
||
588 |
$ psql -U $USER -h $HOST $DB -c "\\copy tbl_name from 'my.csv' csv" |
|
589 |
||
2326 | 590 |
JDBC driver |
591 |
=========== |
|
592 |
||
593 |
https://jdbc.postgresql.org/about/about.html |
|
594 |
The current version of the driver should be compatible with PostgreSQL 8.2 and higher, and Java 6 |
|
595 |
(JDBC 4.0), Java 7 (JDBC 4.1), Java 8 (JDBC 4.2) and Java 9. |
|
596 |
https://jdbc.postgresql.org/download.html |
|
597 |
Java/JDBC/Postgres compatibility table. |
|
598 |
||
2332 | 599 |
plpgsql |
600 |
======= |
|
601 |
||
602 |
Simple function:: |
|
603 |
||
604 |
CREATE OR REPLACE FUNCTION incx(x INTEGER) |
|
605 |
RETURNS INTEGER STABLE AS $$ |
|
606 |
BEGIN |
|
607 |
RETURN x + 1; |
|
608 |
END; |
|
609 |
$$ LANGUAGE plpgsql; |
|
610 |
||
611 |
Support for procedures is added only in Postgres v11. |
|
612 |
||
613 |
https://www.postgresql.org/docs/current/sql-createfunction.html |
|
614 |
``CREATE FUNCTION``. |
|
615 |
https://www.postgresql.org/docs/current/plpgsql-control-structures.html |
|
616 |
plpgsql control structures. |
|
617 |
https://www.postgresql.org/docs/current/errcodes-appendix.html |
|
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
618 |
PostgreSQL Error Codes. |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
619 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
620 |
Anonymous block:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
621 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
622 |
do $$ |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
623 |
begin |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
624 |
null; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
625 |
end $$; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
626 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
627 |
You cannot perform ``select`` without assigning resultset, special keyword ``perform`` is used |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
628 |
insted of ``select`` in PLPQGSL:: |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
629 |
|
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
630 |
do $$ |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
631 |
begin |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
632 |
perform 1 union all select 2; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
633 |
end$$; |
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
634 |
|
2420
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
635 |
Temporary constant table |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
636 |
======================== |
2376
20b65f046002
Revoke privileges and drop role.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2375
diff
changeset
|
637 |
|
2420
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
638 |
Standard SQL syntax:: |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
639 |
|
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
640 |
SELECT 1 AS num, 'a' AS str |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
641 |
UNION ALL SELECT 2, 'b' |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
642 |
UNION ALL SELECT 3, 'c'; |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
643 |
|
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
644 |
Postgre extension:: |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
645 |
|
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
646 |
SELECT num, str FROM ( |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
647 |
VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS tbl(num, str); |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
648 |
|
2421
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
649 |
WITH tbl(num, str) AS (VALUES (1, 'a'), (2, 'b'), (3, 'c')) |
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
650 |
SELECT num, str FROM tbl; |
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
651 |
|
2420
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
652 |
To generate table:: |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
653 |
|
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
654 |
SELECT val FROM generate_series(1, 10) val; |
da418d28831e
Temporary constant table.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2399
diff
changeset
|
655 |
|
2421
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
656 |
WITH t AS (SELECT val FROM generate_series(1, 10) val) |
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
657 |
SELECT * FROM t; |
07304e8a9820
Temporary constant table (with).
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
2420
diff
changeset
|
658 |
|
2422 | 659 |
WITH |
660 |
seq1(num) AS (VALUES (1), (2), (3)), |
|
661 |
seq2(str) AS (VALUES ('a'), ('b'), ('c')) |
|
662 |
SELECT * FROM seq1 |
|
663 |
CROSS JOIN seq2; |
|
664 |