Last active
February 8, 2025 19:04
-
-
Save cernoel/f20eea95f417cdb78c00bb54a7d8bb68 to your computer and use it in GitHub Desktop.
Some scripts for import, export, ... stuff in timescale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
copy_chart_to_csv_gz() { | |
local _backupdir=/var/lib/postgresql/data/backup | |
mkdir -p $_backupdir | |
# user internal path to store gzipped csv | |
docker exec -t --user=postgres $CONTAINER_NAME psql \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="\COPY stocks.chart TO PROGRAM 'gzip > /var/lib/postgresql/data/backup/stocks.chart.csv.gz' WITH delimiter AS ',' CSV HEADER;" | |
} | |
import_chart_from_csv_gz() { | |
local count=$(docker exec -t --user=postgres $CONTAINER_NAME psql -qAt0 \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="SELECT Count(1) WHERE EXISTS (SELECT * FROM stocks.chart LIMIT 1);") | |
echo "result: $count" | |
if [ "${count}" == "0" ]; then | |
echo "empty table found, fetch csv ..." | |
# user internal path to store gzipped csv | |
docker exec -t --user=postgres $CONTAINER_NAME psql \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="\COPY stocks.chart FROM PROGRAM 'zcat /var/lib/postgresql/data/backup/stocks.chart.csv.gz' WITH delimiter AS ',' CSV HEADER;" | |
else | |
echo "table is not empty, will not import csv!" | |
fi; | |
} | |
import_chart_year_from_csv_gz() { | |
echo "in: $1" | |
if [[ "x$1" == "x" ]]; then | |
echo "error: No year provided in arguments." >&2; exit 1 | |
fi | |
if ! [[ $1 =~ ^[0-9]+$ ]] ; then | |
echo "error: Not a number" >&2; exit 1 | |
fi | |
if [ "$1" -gt 2050 ]; then | |
echo "error: Number too great" >&2; exit 1 | |
fi | |
if [ "$1" -lt 1990 ]; then | |
echo "error: Number too ungreat" >&2; exit 1 | |
fi | |
local _year=$1 | |
local _command="SELECT Count(1) WHERE EXISTS (SELECT * FROM stocks.chart WHERE time >= '${_year}-01-01' AND time <= '${_year}-12-31' LIMIT 1);" | |
echo "$_command" | |
local count=$(docker exec -t --user=postgres $CONTAINER_NAME psql -qAt0 \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="$_command") | |
echo "result: $count" | |
if [ "${count}" == "0" ]; then | |
echo "no data for year ${_year} found in table, fetch csv ..." | |
if ! [ -f "./data/backup/stocks.chart.${_year}.csv.gz" ]; then | |
echo "error: file ./data/backup/stocks.chart.${_year}.csv.gz not found" >&2; exit 1 | |
fi | |
local _command="\COPY stocks.chart FROM PROGRAM 'zcat /var/lib/postgresql/data/backup/stocks.chart.${_year}.csv.gz' WITH delimiter AS ',' CSV HEADER;" | |
echo "$_command" | |
docker exec -t --user=postgres $CONTAINER_NAME psql \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="${_command}" | |
else | |
echo "table is not empty, will not import csv!" | |
fi; | |
} | |
all_chart_years=( | |
2013 | |
2014 | |
2015 | |
2016 | |
2017 | |
2018 | |
2019 | |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | |
) | |
export_all_chart_years_to_csv_gz() { | |
for i in "${all_chart_years[@]}"; do | |
echo "export $i" | |
export_chart_year_to_csv_gz $i | |
sleep 2 | |
done | |
} | |
export_chart_year_to_csv_gz() { | |
echo "in: $1" | |
if [[ "x$1" == "x" ]]; then | |
echo "error: No year provided in arguments." >&2; exit 1 | |
fi | |
if ! [[ $1 =~ ^[0-9]+$ ]] ; then | |
echo "error: Not a number" >&2; exit 1 | |
fi | |
if [ "$1" -gt 2050 ]; then | |
echo "error: Number too great" >&2; exit 1 | |
fi | |
if [ "$1" -lt 1990 ]; then | |
echo "error: Number too ungreat" >&2; exit 1 | |
fi | |
local _year=$1 | |
echo "info: got year: $_year" | |
local _command="\COPY (SELECT * FROM stocks.chart WHERE time >= '${_year}-01-01' AND time <= '${_year}-12-31') TO PROGRAM 'gzip > /var/lib/postgresql/data/backup/stocks.chart.${_year}.csv.gz' WITH delimiter AS ',' CSV HEADER;" | |
echo "$_command" | |
docker exec -t --user=postgres $POSTGRES_CONTAINER_NAME psql \ | |
--username="${POSTGRES_USER}" \ | |
--dbname="${POSTGRES_DB}" \ | |
--command="$_command" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment