mkdir docker
mkdir docker/volume
mkdir docker/volume/postgres
touch docker/postgresql.conf
docker run --name postgres_container -e POSTGRES_PASSWORD=password -e POSTGRES_USER=user -v ./docker/volume/postgres:/var/lib/postgresql/data -p 5432:5432 -d postgres
name: postgres
services:
postgres:
container_name: postgres_container
image: postgres
# If permissions error (your folder but docker does not access it) (id -u : id -g)
# user: id:group_id
ports:
- 5432:5432
volumes:
- ./docker/volume/postgress:/var/lib/postgresql/data
- ./docker/postgresql.conf:/etc/postgresql.conf
environment:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: password
For good design, enums should be in the application and not the DB
CREATE TYPE enum_type_name AS ENUM ('case1', 'case2', 'case3');
CREATE TABLE IF NOT EXISTS table (
id SERIAL PRIMARY KEY,
col1 INTEGER,
col2 VARCHAR(255), -- max char length, can be less
col4 TEXT, -- large text (65,535 bytes)
col3 TIMESTAMP NOT NULL DEFAULT NOW(), -- TIMESTAMPZ = with timezone
col4 enum_type_name -- Avoid ENUM in DB, just store the data simply
)
-- 1 item
-- batch items
-- Single column index
CREATE INDEX IF NOT EXISTS idx_columnname ON table (columnname);
-- Mulit column index (composite index) (!! Order is important !!)
CREATE INDEX IF NOT EXISTS index_clumnname1_columnname2 ON table (columnname1, columnname2);
SELECT * FROM table; | |||||||||||||||||||||||||||||||||||||||||
|
|
SELECT col1, col2 FROM table;
SELECT COUNT(id) FROM table; -- COUNT(*) = lenght*width of the table
Same as normal postgres but replace the image by "postgis/postgis"
For better query efficiency on the geometry column
CREATE INDEX table_column_idx ON table USING GIST (column);
CREATE INDEX contries_geom_idx ON contries USING GIST (geom);
SELECT ST_GeomFromGeoJSON('{"coordinates": [6.923589753568848,43.65881928466803],"type": "Point"}') As wkt;
-- wkt => 0101000020E6100000E0FE2983C1B11B40C8CFB83054D44540
SELECT ST_AsText(ST_GeomFromGeoJSON('{"coordinates": [6.923589753568848,43.65881928466803],"type": "Point"}')) As wkt;
-- wkt => POINT(6.923589753568848 43.65881928466803)
SELECT *, geom <-> CONCAT('SRID=4326;', ST_AsText(ST_GeomFromGeoJSON('{"coordinates": [6.923589753568848,43.65881928466803],"type": "Point"}')))::geometry AS dist
FROM grid ORDER BY dist LIMIT 10;
<->
—> Returns the 2D distance between A and B<#>
—> Returns the 2D distance between A and B bounding boxes
# Connect to local db (add -W to ask for password)
psql -d db_name -U username
# Connect to remote db
psql -h url_address -p db_port -d db_name -U username -W
\l
# more info (size)
\l+
# filter by exact db name
\l db_name
\c db_name
\dt
# filter by exact table name
\dt table_name
\d table_name
\dn
\du
\o record_filename
# All your commands/queries. The result will not be displayed
\o
# The commands results will be inside the record_filename file
\q
https://gist.github.com/ArthurDelannoyazerty/575573a0f09769a49ff4f70fe96b0aeb
pg_dump -f /pg_dump.dump -Fc --dbname DB_NAME -U USERNAME
pg_restore --dbname DB_NAME -c -C -O --no-acl -U USERNAME /pg_dump.dump