Jump to content

Linux/Postgres: Difference between revisions

From Wiki
Marcluer (talk | contribs)
mNo edit summary
Marcluer (talk | contribs)
 
(12 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 107: Line 113:
!
!
!
!
!Example
|-
|-
|=
|=
|equal
|equal
|WHERE "time" = <code>'2024-09-28 15:46:09.602492+00'</code>
|-
|-
|<>, !=
|<>, !=
|not equal
|not equal
|
|-
|-
|<, >
|<, >
|less/greater than
|less/greater than
|
|-
|-
|<=, >=
|<=, >=
|less/greater than or equal
|less/greater than or equal
|
|-
|
|
|
|-
|-
|IS NULL
|
|
|-
|
|
|
|
|
|-
|-
|LIKE
|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
|
|
|
|
Line 134: Line 204:
<blockquote>
<blockquote>
<pre>
<pre>
\l
SELECT datname FROM pg_database
SELECT datname FROM pg_database
WHERE datistemplate = false;
WHERE datistemplate = false;
</pre>
</blockquote>
* Create database
<blockquote>
<pre>
CREATE DATABASE newdb OWNER user;
GRANT ALL PRIVILEGES ON DATABASE newdb TO user;
</pre>
</blockquote>
* Connect to database
<blockquote>
<pre>
\c newdb
</pre>
</pre>
</blockquote>
</blockquote>
Line 143: Line 230:
<blockquote>
<blockquote>
<pre>
<pre>
\dt
\dt *.*
SELECT table_catalog,table_schema,table_name FROM information_schema.tables  
SELECT table_catalog,table_schema,table_name FROM information_schema.tables  
WHERE table_type = 'BASE TABLE' AND table_schema = 'public'  
WHERE table_type = 'BASE TABLE' AND table_schema = 'public'  
Line 148: Line 238:
</pre>
</pre>
</blockquote>
</blockquote>
* View table information/structure
<blockquote>
<pre>
\d new_table
</pre>
</blockquote>
* Number of rows in table
<blockquote>
<pre>
SELECT COUNT(*) FROM process_value_numeric;
</pre>
</blockquote>


* Biggest tables by size
* Biggest tables by size
Line 171: Line 276:


== Links ==
== Links ==
* https://www.w3schools.com/postgresql/
* https://www.timescale.com/learn/postgres-cheat-sheet
* https://www.timescale.com/learn/postgres-cheat-sheet
* https://www.timescale.com/learn/postgres-basics
* https://www.timescale.com/learn/postgres-basics

Latest revision as of 16:52, 12 January 2025

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
\l

SELECT datname FROM pg_database
WHERE datistemplate = false;
  • Create database
CREATE DATABASE newdb OWNER user;
GRANT ALL PRIVILEGES ON DATABASE newdb TO user;
  • Connect to database
\c newdb

Tables

  • List all tables
\dt
\dt *.*

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
  • View table information/structure
\d new_table
  • Number of rows in table
SELECT COUNT(*) FROM process_value_numeric;


  • 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;

Links