├── postgresqlite └── readme.md /postgresqlite: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior. 3 | 4 | DATADIR=$(realpath "$1") 5 | CONTAINER_NAME="postgres-${DATADIR//[^a-zA-Z0-9_.-]/_}" 6 | IMAGE='postgres:latest' 7 | COMMAND='' 8 | if [ -n "${2-}" ]; then 9 | COMMAND="-c \"$2\"" 10 | fi 11 | 12 | if [ -t 0 ]; then 13 | # used interactively 14 | INTERACTIVE="-it" 15 | else 16 | # got stdin 17 | INTERACTIVE="-i" 18 | fi 19 | 20 | POSTGRES_OPTS="-c 'max_connections=200' -c 'shared_buffers=4GB' -c 'effective_cache_size=12GB' -c 'maintenance_work_mem=1GB' -c 'checkpoint_completion_target=0.7' -c 'wal_buffers=16MB' -c 'default_statistics_target=100' -c 'random_page_cost=1.1' -c 'effective_io_concurrency=200' -c 'work_mem=1GB' -c 'min_wal_size=1GB' -c 'max_wal_size=2GB' -c 'max_worker_processes=8' -c 'max_parallel_workers_per_gather=8' -c 'max_parallel_workers=8'" 21 | 22 | 23 | mkdir -p "$DATADIR" 24 | docker run --rm \ 25 | "$INTERACTIVE" \ 26 | --name "$CONTAINER_NAME" \ 27 | -v "$DATADIR":/var/lib/postgresql/data \ 28 | -e POSTGRES_PASSWORD=mysecretpassword \ 29 | --user $UID \ 30 | --shm-size=8g \ 31 | "$IMAGE" \ 32 | bash -c "(./docker-entrypoint.sh postgres $POSTGRES_OPTS &) &>/dev/null; until pg_isready -U postgres &>/dev/null; do sleep 0.1; done; psql -U postgres -d postgres --echo-all -v ON_ERROR_STOP=1 $COMMAND; EXIT_CODE=\$?; pg_ctl stop &>/dev/null; exit \$EXIT_CODE" 33 | 34 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # PostgreSQLite 2 | 3 | Use postgres like sqlite. 4 | 5 | # Example Usage 6 | 7 | ```bash 8 | $ postgresqlite test-db "create table numbers(id serial primary key, number real not null)" 9 | create table numbers(id serial primary key, number real not null) 10 | CREATE TABLE 11 | 12 | $ postgresqlite test-db "insert into numbers (number) values (5)" 13 | insert into numbers (number) values (5) 14 | INSERT 0 1 15 | 16 | $ postgresqlite test-db "select * from numbers" 17 | select * from numbers 18 | id | number 19 | ----+-------- 20 | 1 | 5 21 | (1 row) 22 | 23 | $ du -sh test-db 24 | 40M test-db 25 | ``` 26 | 27 | 28 | 29 | # Install 30 | 31 | Requires: `docker`. 32 | 33 | Add the postgresqlite script to your path. 34 | 35 | ```bash 36 | # for example 37 | PATH=$PATH:$HOME/projects/postgresqlite 38 | ``` 39 | 40 | 41 | --------------------------------------------------------------------------------