Skip to content

Instantly share code, notes, and snippets.

@iver
iver / psql_task
Created October 22, 2014 14:35
PSQL common task
-----------
-- Drop database connections
-----------
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'database_name'
AND pid <> pg_backend_pid();
-----------
@iver
iver / diff_date.go
Created November 11, 2014 19:45
Year difference between two dates in go
package main
import "fmt"
import "time"
func main() {
birth, _ := time.Parse("2006-01-02", "2000-11-10")
duration := time.Since(birth)
t1 := time.Unix(0,duration.Nanoseconds())
// Remember, Thursday, 1 January 1970 is defined as first unix day
@iver
iver / update_sequence.sql
Created August 6, 2015 23:58
Update sequence query
SELECT
'select pg_catalog.setval(pg_get_serial_sequence('''||
pg_class.relname || ''', ''' ||
pg_attribute.attname || '''), (SELECT MAX(' || pg_attribute.attname || ') FROM ' ||
pg_class.relname || ') +1 ); '
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = pg_class.relname::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
@iver
iver / Makefile
Last active June 3, 2016 00:12
Makefile for phoenix project
SHELL := /bin/bash
ACTUAL := $(shell pwd)
MIX_ENV=dev
export MIX_ENV
export ACTUAL
all: release
clean:
@iver
iver / deploy.sh
Created June 3, 2016 01:07
Deploy script for phoenix project
#!/bin/bash
export PROJECT=stats
export RELEASE=${PROJECT}.tar.gz
PROG_NAME=$(basename $0)
while getopts :s:v: OPTION
do
case ${OPTION} in
s) TARGET=${OPTARG};;
@iver
iver / curl.md
Created September 9, 2017 13:48 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@iver
iver / SSL-command-line.md
Last active November 16, 2017 04:31
Validate SSL Certificate from command line

Determine SSL cert expiration date from a PEM file:

$ openssl x509 -enddate -noout -in file.pem

Check many files:

for pem in /etc/ssl/certs/*.pem; do 
@iver
iver / postgres_queries_and_commands.sql
Last active November 30, 2022 19:26 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(query_start, clock_timestamp()), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(query_start, clock_timestamp()), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@iver
iver / upgrade_postgres.md
Last active June 7, 2018 14:50 — forked from giannisp/gist:ebaca117ac9e44231421f04e7796d5ca
Upgrade PostgreSQL 9.6.5 to 10.0 using Homebrew (macOS)

NOTES: After automatically updating Postgres to 10.0 via Homebrew, the pg_ctl start command didn't work. The error was "The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.0."

Database files have to be updated before starting the server, here are the steps that had to be followed:

  • Need to have both 9.6.x and latest 10.0 installed, and keep 10.0 as default
brew unlink postgresql
brew install postgresql@9.6
brew unlink postgresql@9.6
@iver
iver / postgres-cheatsheet.md
Last active September 14, 2020 18:40 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

$ psql -U postgres

or

$ sudo -u postgres psql