Skip to content

Instantly share code, notes, and snippets.

View ichux's full-sized avatar
🏠
Working from home

Chukwudi Nwachukwu ichux

🏠
Working from home
View GitHub Profile
@ichux
ichux / dropdb.sql
Created September 18, 2018 09:37
Drop DB in Postgres
-- Terminate any existing connections to `thedb`
SELECT
pg_terminate_backend(pg_stat_activity.pid)
FROM
pg_stat_activity
WHERE
pg_stat_activity.datname = 'thedb'
AND pid <> pg_backend_pid();
-- Now go ahead and drop the database, `thedb`
@ichux
ichux / rowtojson.sql
Created September 18, 2018 09:41
Converts Posgres rows to json
SELECT
row_to_json(details)
FROM
(
SELECT
ARRAY[ 1,
2,
3 ] as arrays,
'Some information' AS text
) details
@ichux
ichux / date-calculations.sql
Created September 18, 2018 10:40
add and subtract Postgres dates
select date '2018-09-18' - date '1990-11-22' AS difference;
select date '1990-11-22' + 10162 AS difference;
@ichux
ichux / play-with-char.sql
Created September 18, 2018 10:57
playing with Postgre characters
SELECT ASCII('<'), CHR(60), regexp_split_to_array(md5(random() :: TEXT), '') AS value
FROM generate_series(1, 3)
ORDER BY random()
@ichux
ichux / drop-triggers-functions.sql
Last active September 19, 2018 09:13
single call to drop specific triggers and functions in Postgres
-- get functions
SELECT 'DROP FUNCTION IF EXISTS ' || ROUTINE_NAME || ' CASCADE;'
FROM information_schema.routines
WHERE routine_type = 'FUNCTION'
AND specific_schema = 'public'
AND ROUTINE_NAME IN ('ichux')
UNION
-- get triggers
SELECT 'DROP TRIGGER IF EXISTS ' || TRIGGER_NAME || ' ON ' || event_object_table || ';'
FROM information_schema.triggers
@ichux
ichux / first_and_last_day.sql
Last active September 22, 2018 20:02
Get the first and last date of the present month in Postgres
WITH first_day AS (SELECT date_trunc('MONTH', NOW()) :: DATE),
last_day AS (SELECT (date_trunc('MONTH', (SELECT * FROM first_day)) + INTERVAL '1 MONTH' -
INTERVAL '1 DAY') :: DATE)
SELECT first_day.*, last_day.*
FROM first_day,
last_day;
@ichux
ichux / list_months.sql
Created October 2, 2018 03:14
lists all the months in a given year interval et al
SELECT month_day1 :: DATE AS month_starts,
(month_day1 + INTERVAL '1 month' - INTERVAL '1 day') :: DATE AS month_ends,
(month_day1 + INTERVAL '1 month') :: DATE AS next_month_starts,
(month_day1 + INTERVAL '1 month') :: DATE - month_day1 :: DATE AS days
FROM generate_series(DATE '2018-01-01', DATE '2018-12-01', INTERVAL '1 month') AS month_day1;
@ichux
ichux / first_last.sql
Created October 2, 2018 03:15
row_to_json to get all the first and last days of the current month
WITH first_day AS (SELECT date_trunc('MONTH', NOW()) :: DATE AS first_date),
last_day AS (SELECT (date_trunc('MONTH', (SELECT * FROM first_day)) + INTERVAL '1 MONTH' -
INTERVAL '1 DAY') :: DATE AS last_date)
SELECT row_to_json(first_last)
FROM (SELECT first_day.*, last_day.*
FROM first_day,
last_day) first_last;
@ichux
ichux / echo-formatted.sh
Last active June 30, 2021 10:36
echo json to formatted output
#!/bin/sh
printf "\033c"
echo '{
"company": "PITCH CARDINAL CODING LIMITED",
"rc_no": "1387119",
"established": "2017-01-25",
"address": "1B, MARKET STREET, OYINGBO"
}' | python -m json.tool # > result.json
@ichux
ichux / sm25.py
Last active November 5, 2018 09:32
send localmails
import email.utils
import smtplib
from email.mime.text import MIMEText
# Create the message
msg = MIMEText('Hello SMTPD!')
msg['To'] = email.utils.formataddr(("Local Mail", "localmail@localhost"))
msg['From'] = email.utils.formataddr(('Chukwudi Nwachukwu', 'chukwudinwachukwu@localhost'))
msg['Subject'] = 'Local mail using SMTP'