Skip to content

Instantly share code, notes, and snippets.

View cherts's full-sized avatar

Mikhail Grigorev cherts

View GitHub Profile
@cherts
cherts / test_postgis.sql
Created June 5, 2024 14:26
Simple test for PostGIS extension for PostgreSQL
-- Show PostGIS version
SELECT PostGIS_version();
-- Create table
CREATE TABLE global_points (
id SERIAL PRIMARY KEY,
name VARCHAR(64),
location geography(POINT,4326)
);
@cherts
cherts / get_root_ca_percona_everest.sh
Created May 30, 2024 08:07
Get root ca for Percona Everest (PostgreSQL)
#/bin/bash
EVEREST_ENV=prod
mkdir ~/.postgresql
touch ~/.postgresql/root.crt
kubectl get secrets/pgo-root-cacert -n ${EVEREST_ENV} -o jsonpath="{.data['root\.crt']}" | base64 -d >> ~/.postgresql/root.crt
# psql "host=127.0.0.1 port=5432 dbname=postgres user=postgres password=XXXXXX sslmode=prefer"
@cherts
cherts / xtrabackup_init_replication.sh
Last active May 28, 2024 14:26
Init MySQL replication after xtrabackup prepare (support gtid with multiline)
#!/bin/bash
MYSQL_MASTER_HOST=X.X.X.X
MYSQL_REPL_USER_NAME="repl"
MYSQL_REPL_USER_PASSWORD="bigpassword"
XTRABACKUP_INFO=/mnt/backup/xtrabackup_info
XTRABACKUP_BINLOG_INFO=/mnt/backup/xtrabackup_binlog_info
USE_AUTOPOSITION=1
if [ ! -f "${XTRABACKUP_INFO}" ]; then
@cherts
cherts / yc_install.sh
Last active May 13, 2024 07:14
Install Yandex.Cloud CLI (Linux/MacOS)
curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash -s sudo -- -n
@cherts
cherts / pg_run_analyze_all_table.sql
Created April 2, 2024 08:38
PostgreSQL run analyze all table (using pl/pgsql script)
DO $$
DECLARE
tab RECORD;
schemaName VARCHAR := 'public';
BEGIN
for tab in (SELECT t.relname::varchar AS table_name
FROM pg_class t
JOIN pg_namespace n ON n.oid = t.relnamespace
WHERE t.relkind = 'r' and n.nspname::varchar = schemaName
ORDER BY 1)
@cherts
cherts / pg_create_database_if_not_exists
Created March 27, 2024 13:50
CREATE DATABASE NOT EXISTS in PostgreSQL (workaround from within psql)
# native using psql
SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec
# native using shell + psql
echo "SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec" | psql
@cherts
cherts / mysql_get_table_count.sql
Created March 27, 2024 13:26
Getting the number of records in all tables in MySQL (use count)
SET GROUP_CONCAT_MAX_LEN=131072;
SET @selects = NULL;
SELECT CONCAT('SELECT * FROM (\n', GROUP_CONCAT(single_select SEPARATOR ' UNION\n'), '\n ) Q ORDER BY Q.TABLE_ROWS DESC') AS sql_query
FROM (
SELECT CONCAT(
'SELECT "',
table_name,
'" AS TABLE_NAME, COUNT(1) AS TABLE_ROWS
FROM `',
table_schema,
@cherts
cherts / pg_get_table_count.sql
Created March 27, 2024 11:52
Getting the number of records in all tables in PostgreSQL (use count)
WITH tbl AS (
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name NOT LIKE 'pg_%' AND table_schema NOT IN ('information_schema')
)
SELECT table_schema, table_name,
(xpath('/row/c/text()', query_to_xml(format('SELECT count(1) AS c FROM "%I"."%I"', table_schema, table_name), FALSE, TRUE, '')))[1]::text::bigint AS table_count
FROM tbl;
@cherts
cherts / gitignore_global
Last active March 14, 2024 11:30
Global Git excludes file
git config --global core.excludesfile ~/.gitignore_global
cat <<EOF > ~/.gitignore_global
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
EOF
@cherts
cherts / .vimrc
Created January 26, 2024 06:54
My .vimrc file
vnoremap :w !xsel -b
" Make backspace delete lots of things
" set backspace=indent,eol,start
" Enable syntax highlighting
syntax on
" Enable line highlight
set cursorline
" Try to show at least three lines and two columns of context when
" scrolling
set scrolloff=3