Základní definice

Definice dle SQL standardu:

Databázový server je cluster. (DBMS) Cluster má catalogy. (Catalog = Database) Catalogy obsahují schemata (=workspace). Schemata obsahují tabulky. Tabulky obsahují řádky. Řádky obsahují data, definovaná pomocí sloupečků.

Základní příkazy pro kontrolu a nastavení

  • \list nebo \l (výpis databází)
$ psql
psql (15.5 ( 15.5-0+deb12u1))
Pro získání nápovědy napište "help".

user=# \list

                                                  Seznam databází
   Jméno   | Vlastník | Kódování |  Collation  |    CType    | ICU Locale | Locale Provider |   Přístupová práva    
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | cs_CZ.UTF-8 | cs_CZ.UTF-8 |            | libc            | 
 rpi       | postgres | UTF8     | cs_CZ.UTF-8 | cs_CZ.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | cs_CZ.UTF-8 | cs_CZ.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | cs_CZ.UTF-8 | cs_CZ.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 test      | rpi      | UTF8     | cs_CZ.UTF-8 | cs_CZ.UTF-8 |            | libc            | 
(5 řádek)

You cannot to change these values for already created databases. In this moment, when there are not other databases, the most easy solution is a) stop database, b) delete data directory, c) run manually initdb with options --encoding and --locale (run this command under postgres user). if you have some data there, backup (with pg_dump) first.

You can change these parameters when you create new database too (this is SQL command):

CREATE DATABASE test ENCODING='UTF8' LOCALE='C' TEMPLATE='template0';
Locale can be overwritten by clause COLLATE used in CREATE TABLE or SELECT commands.

nebo vytvořit databazi s lokalizačními parametry

createdb -E UTF8 -T template0 --lc-collate=cs_CZ.UTF-8 --lc-ctype=cs_CZ.UTF-8 rimmer
  • Poznámky

Používejte kódování UTF-8. Možná by se vám mohlo ve Windows lépe pracovat s WIN1250, ale UTF-8 je prostě modernější a poradí si s většinou jazyků, se kterými se můžete setkat.

LC_COLLATE určuje, jak se budou znaky řadit. Čeština má například tu specialitu, že ch je v abecedě před cd, protože ch se bere jako jedno písmeno. Používejte pro COLLATE cs_CZ.UTF-8, pokud je to možné (a vhodné).

Postgres umí „tradiční C“ COLLATE (normální řazení a-z), které se jmenuje C a pak POSIX (to je nějaká norma). Další podpora COLLATE je závislá na operačním systému, takže se může taky stát, že cs_CZ.UTF-8 nebudete mít dostupné.

LC_CTYPE určuje další informace o znacích (např. co je znak, co je číslo, co je velké a co malé písmeno atd.). LC_CTYPE byste ani nemuseli zadávat, ono se k LC_COLLATE vybere tak nějak samo to správné.

SQL

SQLite

View/Pohled je prakticky náhrada za výpočet ve FileMakeru. Zásadní rozdíl je, že se vytváří mimo původní tabulku a vzniká jiná tabulka pro View/Pohled, takže v grafickém vyjádření je to poněkud pracnější.

Postup instalace


~ % brew install postgresql@14
...

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@14
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql@14 after an upgrade:
  brew services restart postgresql@14
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14
==> Summary
🍺  /opt/homebrew/Cellar/postgresql@14/14.7: 3,314 files, 45.2MB
==> Running `brew cleanup postgresql@14`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> postgresql@14
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@14
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql@14 after an upgrade:
  brew services restart postgresql@14
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14
  
  ...

brew services start postgresql@14
==> Successfully started `postgresql@14` (label: homebrew.mxcl.postgresql@14)
~ % brew services list            
Name          Status  User   File
postgresql@14 started karel4 ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist
~ % 

...

~ % psql postgres
psql (14.7 (Homebrew))
Type "help" for help.

postgres=# 

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 karel4    | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# 

postgres=# \l
                          List of databases
   Name    | Owner  | Encoding | Collate | Ctype | Access privileges 
-----------+--------+----------+---------+-------+-------------------
 postgres  | wlado1 | UTF8     | C       | C     | 
 template0 | wlado1 | UTF8     | C       | C     | =c/karel4        +
           |        |          |         |       | karel4=CTc/karel4
 template1 | wlado1 | UTF8     | C       | C     | =c/wlado1        +
           |        |          |         |       | karel4=CTc/karel4
(3 rows)

CREATE DATABASE adresy with
        OWNER = karel4
        ENCODING = 'UTF8'
        LC_COLLATE = 'cs_CZ.UTF-8'
        LC_CTYPE = 'cs_CZ.UTF-8'
        TABLESPACE = pg_default
        TEMPLATE='template0'

        CONNECTION LIMIT = -1;


Jak vložit prázný datum do tabulky

select 'infinity'::timestamp, '-infinity'::timestamp

 timestamp | timestamp 
-----------+-----------
 infinity  | -infinity
(1 row)
TOP