mkdir docker
mkdir docker/volume
mkdir docker/volume/postgres
touch docker/postgresql.confdocker 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: passwordFor 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);# With a geojson python dict such as
geom = {
"type":"Polygon",
"coordinates":[[[13.861471,37.942081],[15.111086,37.947537],[15.109632,36.95783],[13.87637,36.952565],[13.861471,37.942081]]]
}
# You just need to do
str_geom = json.dumps(geom)
# And put it into the following SQL command INSERT INTO table1 ("zone") VALUES (ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326));SELECT ST_AsGeoJSON(zone) FROM table1;# You receive the string of the geojson
from shapely import shape
geojson = json.loads(response[0][0])
geom = shape(geojson)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\qhttps://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
sql size tables (Modify "WHERE table_schema = 'public'" if needed).
Output Example :