Linux/Postgres: Difference between revisions
< Linux
mNo edit summary |
m (→Operators) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Comment == | |||
<pre> | |||
-- single line comment | |||
/* multi line comment */ | |||
</pre> | |||
== Data types == | == Data types == | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 102: | Line 108: | ||
|} | |} | ||
== List all databases | == Operators == | ||
{| class="wikitable" | |||
|+ | |||
! | |||
! | |||
!Example | |||
|- | |||
|= | |||
|equal | |||
|WHERE "time" = <code>'2024-09-28 15:46:09.602492+00'</code> | |||
|- | |||
|<>, != | |||
|not equal | |||
| | |||
|- | |||
|<, > | |||
|less/greater than | |||
| | |||
|- | |||
|<=, >= | |||
|less/greater than or equal | |||
| | |||
|- | |||
| | |||
| | |||
| | |||
|- | |||
|IS NULL | |||
| | |||
| | |||
|- | |||
| | |||
| | |||
| | |||
|- | |||
|LIKE | |||
| | |||
|WHERE "time | |||
|- | |||
| | |||
|_ = single char | |||
|escaped = \_ | |||
|- | |||
| | |||
|% = 0 or more chars | |||
|escaped = \% | |||
|- | |||
|ILIKE | |||
|case-insensitive LIKE | |||
| | |||
|- | |||
| | |||
| | |||
| | |||
|- | |||
|IN | |||
| | |||
|WHERE name IN ('Joe', 'Bob', 'Jane') | |||
|- | |||
| | |||
| | |||
|WHERE user_id IN (SELECT user_id FROM orders) | |||
|- | |||
| | |||
| | |||
| | |||
|- | |||
|~ | |||
|regex | |||
| | |||
|- | |||
|!~ | |||
|not regex | |||
| | |||
|- | |||
| | |||
| | |||
| | |||
|- | |||
|AND | |||
| | |||
| | |||
|- | |||
|OR | |||
| | |||
| | |||
|- | |||
|BETWEEN | |||
| | |||
| | |||
|} | |||
== Databases == | |||
* List all databases | |||
<blockquote> | <blockquote> | ||
<pre> | <pre> | ||
Line 110: | Line 209: | ||
</blockquote> | </blockquote> | ||
== List all tables | == Tables == | ||
* List all tables | |||
<blockquote> | <blockquote> | ||
<pre> | <pre> | ||
Line 116: | Line 216: | ||
WHERE table_type = 'BASE TABLE' AND table_schema = 'public' | WHERE table_type = 'BASE TABLE' AND table_schema = 'public' | ||
ORDER BY table_type, table_name | ORDER BY table_type, table_name | ||
</pre> | |||
</blockquote> | |||
* Biggest tables by size | |||
<blockquote> | |||
<pre> | |||
SELECT | |||
nspname || '.' || relname AS "Object Name", relkind As "Object Type", | |||
pg_size_pretty(pg_relation_size(C.oid)) AS "size" | |||
FROM pg_class C | |||
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) | |||
WHERE nspname NOT IN ('pg_catalog', 'information_schema') | |||
ORDER BY pg_relation_size(C.oid) DESC | |||
LIMIT 20; | |||
</pre> | </pre> | ||
</blockquote> | </blockquote> | ||
Line 126: | Line 240: | ||
</blockquote> | </blockquote> | ||
== Links == | |||
* https://www.w3schools.com/postgresql/ | |||
* https://www.timescale.com/learn/postgres-cheat-sheet | |||
* https://www.timescale.com/learn/postgres-basics | |||
* https://www.timescale.com/learn/postgres-guides | |||
* https://www.timescale.com/learn/postgres-best-practices | |||
[[Category:Linux/Services]] | [[Category:Linux/Services]] | ||
[[Category:Linux]] | [[Category:Linux]] | ||
[[Category:Programming]] | [[Category:Programming]] |
Latest revision as of 15:18, 11 October 2024
Comment
-- single line comment /* multi line comment */
Data types
Type | min | max | comment |
---|---|---|---|
boolean | |||
char(n) | |||
varchar(n) | |||
text | |||
smallint | -32768 | 32767 | |
int | |||
serial | (~auto_increment in mysql) | ||
float(n) | |||
real / float8 | |||
numeric(p,s) | |||
date | |||
time | |||
timestamp | |||
timestamptz | |||
interval | |||
json | |||
jsonb | |||
uuid | |||
+ special |
Operators
Example | ||
---|---|---|
= | equal | WHERE "time" = '2024-09-28 15:46:09.602492+00'
|
<>, != | not equal | |
<, > | less/greater than | |
<=, >= | less/greater than or equal | |
IS NULL | ||
LIKE | WHERE "time | |
_ = single char | escaped = \_ | |
% = 0 or more chars | escaped = \% | |
ILIKE | case-insensitive LIKE | |
IN | WHERE name IN ('Joe', 'Bob', 'Jane') | |
WHERE user_id IN (SELECT user_id FROM orders) | ||
~ | regex | |
!~ | not regex | |
AND | ||
OR | ||
BETWEEN |
Databases
- List all databases
SELECT datname FROM pg_database WHERE datistemplate = false;
Tables
- List all tables
SELECT table_catalog,table_schema,table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = 'public' ORDER BY table_type, table_name
- Biggest tables by size
SELECT nspname || '.' || relname AS "Object Name", relkind As "Object Type", pg_size_pretty(pg_relation_size(C.oid)) AS "size" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') ORDER BY pg_relation_size(C.oid) DESC LIMIT 20;
List all users
SELECT * FROM pg_user;