Skip to content

Instantly share code, notes, and snippets.

@pilgrim2go
pilgrim2go / pg_change_db_owner.sh
Created January 14, 2016 03:25 — forked from jirutka/pg_change_db_owner.sh
Some convenient scripts to manage ownerships and privileges in PostgreSQL.
#!/bin/bash
#
# The MIT License
#
# Copyright 2014 Jakub Jirutka <jakub@jirutka.cz>.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@pilgrim2go
pilgrim2go / csv2json.sh
Created February 15, 2016 09:46
CSV 2 JSON in Bash
#!/bin/bash
# CSV to JSON converter using BASH
# Usage ./csv2json input.csv > output.json
# http://blog.secaserver.com/2013/12/convert-csv-json-bash/
input=$1
[ -z $1 ] && echo "No CSV input file specified" && exit 1
[ ! -e $input ] && echo "Unable to locate $1" && exit 1
@pilgrim2go
pilgrim2go / AWS CloudFormation SNS Subscription.json
Created July 1, 2016 07:50
AWS CloudFormation SNS Subscription
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Topic": {
"Type": "AWS::SNS::Topic",
"Properties": {
}
},
"Queue": {
"Type": "AWS::SQS::Queue",
# #!/usr/bin/python2
# From https://gist.github.com/iomz/9774415
# Adapt to use boto3
import boto3
import botocore
from time import sleep
import sys
import os
if len(sys.argv) != 3:
@pilgrim2go
pilgrim2go / plpythonu.sql
Created October 27, 2016 11:16 — forked from rturowicz/plpythonu.sql
postgresql: example use of python procedural language
-- query with stored plan
CREATE or replace FUNCTION pybench1(id int) RETURNS text AS '
if (SD.has_key("plan")):
plan = SD["plan"]
else:
plan = plpy.prepare("SELECT * FROM pagetimer pt, pagebrowser pb WHERE pt.idtimer = $1 and pt.idtimer = pb.idtimer", ["int4"])
SD["plan"] = plan
rec = plpy.execute(plan, [id])
if (rec.nrows() > 0):
@pilgrim2go
pilgrim2go / turnstile_funct.py
Created October 27, 2016 11:18 — forked from sethc23/turnstile_funct.py
f(x) z_get_string_dist
def z_get_string_dist(self):
cmd="""
DROP TYPE IF EXISTS string_dist_results cascade;
CREATE TYPE string_dist_results as (
idx integer,
orig_str text,
jaro double precision,
jaro_b text,
leven integer,
create or replace function update_asset_json(text, text, integer) returns setof integer as $$
import simplejson
from datetime import datetime
now = datetime.now()
updated_data = simplejson.loads(args[1])
rs = plpy.execute("select id, data from assets where id in (%s)" % args[0])
resp = []
for r in rs:
data = simplejson.loads(r['data'])
data.update(updated_data)
@pilgrim2go
pilgrim2go / set_sequence_values.py
Created October 27, 2016 11:26 — forked from BookLaugh/set_sequence_values.py
PostgreSQL procedure for updating all sequences used as default values for table columns. Once called, it will update values of sequences to max value present in column plus 5 (why 5? So you can find out which sequences were altered). Function returns list of names of updated sequences.
CREATE OR REPLACE FUNCTION set_seq_vals(schema_name TEXT)
RETURNS TEXT[]
AS $$
schemaname = schema_name if schema_name else 'public'
plpy.execute("SET SEARCH_PATH=%s,public" % schemaname)
tables = plpy.execute("SELECT tablename FROM pg_tables WHERE schemaname = '%s'" % schemaname);
altered_seqs = []
for t in tables:
cols = plpy.execute("SELECT column_name, column_default, data_type, is_nullable FROM information_schema.columns WHERE table_name = '%s' AND data_type in ('integer', 'bigint') AND column_default IS NOT NULL" % t["tablename"])
for c in cols:
@pilgrim2go
pilgrim2go / run_server.pl.py
Created October 27, 2016 11:27 — forked from joshwilliams/run_server.pl.py
An httpd-like thing in a PL/Python function
CREATE OR REPLACE FUNCTION run_server() RETURNS text
LANGUAGE plpythonu STRICT
AS $$
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class PGHandler(BaseHTTPRequestHandler):
def do_GET(self):
plpy.notice("Path: %s" % self.path)
if self.path.count('/') == 2 and self.path.split('/')[1] != "" and self.path.split('/')[2] != "":
nsp = plpy.quote_ident(self.path.split('/')[1])
@pilgrim2go
pilgrim2go / pgmail.sql
Created October 27, 2016 11:28 — forked from mikaelhg/pgmail.sql
PostgreSQL PL/Python stored procedures for email handling
CREATE OR REPLACE FUNCTION download(url TEXT) RETURNS TEXT
AS $$
import urllib2
try:
result = urllib2.urlopen(url)
rawdata = result.read()
info = result.info()
try:
content_type, encoding = info['Content-Type'].split('charset=')
except: