Skip to content

Instantly share code, notes, and snippets.

@nathansgreen
nathansgreen / new-mac-iinit.sh
Last active February 26, 2024 15:02
Initial setup for a new MacOS install
#!/usr/bin/env sh
# stuff to do when setting up a fresh install of MacOS
set -o errexit -o nounset -o noclobber
[ -e /etc/pam.d/sudo_local ] || \
echo 'auth sufficient pam_tid.so' \
| sudo tee /etc/pam.d/sudo_local
softwareupdate --install-rosetta --agree-to-license
@nathansgreen
nathansgreen / example.sql
Last active January 24, 2024 23:06
Postgres Sequence Bulk Increment
-- I needed a way to safely pull a bunch of sequence values to my client in order to bulk
-- insert many thousands of rows. I'm doing this because I don't want to use 128-bit keys.
-- I found this:
-- https://www.depesz.com/2008/03/20/getting-multiple-values-from-sequences/
select pg_advisory_lock(123);
alter sequence seq increment by 1000;
select nextval('seq');
alter sequence seq increment by 1;
select pg_advisory_unlock(123);
@nathansgreen
nathansgreen / docker-compose.yml
Created December 4, 2023 17:39
UniFi Network Application with FerretDB
version: "3.8"
services:
unifi-controller:
image: lscr.io/linuxserver/unifi-network-application:7.5.187
restart: unless-stopped
depends_on:
- ferretdb
environment:
PUID: 1000
PGID: 1000
@nathansgreen
nathansgreen / last_insert_test.sql
Last active August 4, 2023 04:42
MySQL LAST_INSERT_ID used with On Duplicate Key Update
/*
* Quick demonstration of `id = LAST_INSERT_ID(id)` being the key to returning
* the existing id when doing `ON DUPLICATE KEY UPDATE`. The unfortunate side
* effect of this approach is that the sequence number for `id` increments on
* every update, even though the value for the updated row does not change. On
* update-heavy systems with 32-bit id`s, the sequence could be exhausted in a
* fairly short amount of time.
*
* Just switch to MariaDB and use `RETURNING id` instead. PostgreSQL got this
* keyword in 2006. Oracle was doing this in procedural code no later than 1997.
@nathansgreen
nathansgreen / array_position.sql
Created January 5, 2018 16:57
PostgreSQL <9.5 array_position function
-- The array_position function was added in Postgres 9.5.
-- For older versions, you can get the same behavior with this function.
create function array_position(arr ANYARRAY, elem ANYELEMENT, pos INTEGER default 1) returns INTEGER
language sql
as $BODY$
select row_number::INTEGER
from (
select unnest, row_number() over ()
from ( select unnest(arr) ) t0
) t1
@nathansgreen
nathansgreen / icloud-evict.sh
Last active May 25, 2023 17:28
Fix macOS not enough free space to update
#!/bin/sh
# Manually free up local copies of files backed up to iCloud.
path=${1:-*}
pushd "~/Library/Mobile Documents/com~apple~CloudDocs" \
&& brctl evict $path
@nathansgreen
nathansgreen / config
Created February 13, 2023 20:21
.ssh/config
IdentitiesOnly=yes
ConnectTimeout=10
ConnectionAttempts=1
Host *
UseKeychain yes
GSSAPIAuthentication no
Host 10.245.*
User ec2-user
IdentityFile ~/.ssh/aws.pem
@nathansgreen
nathansgreen / postgresql-debugger-install-macos
Last active January 18, 2023 04:04 — forked from jhngrant/postgresql-debugger-install-ubuntu
Installing the PL/pgSQL Debugger Extension (pldbgapi) for pgAdmin III on PostgreSQL 9.4 and MacOS
# First install database
brew install postgres
# Clone and build the PL/pgSQL server-side debugger
srcdir=/usr/local/src
[ -e "$scrdir" ] || \
sudo sh -c "mkdir $srcdir && chgrp admin $srcdir && chmod g+w $srcdir"
cd "$srcdir"
@nathansgreen
nathansgreen / gs-resample.sh
Created December 22, 2022 16:17 — forked from lkraider/gs-resample.sh
Ghostscript PDF quality downsample
#!/bin/sh
# It seems it's very hard to set resample output quality with Ghostscript.
# So instead rely on `prepress` preset parameter to select a good /QFactor
# and override the options we don't want from there.
gs \
-o resampled.pdf \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
@nathansgreen
nathansgreen / usg-commands.md
Last active November 6, 2022 00:17
USG CLI commands

Ubiquit Security Gateway CLI Commands

Random commands I've used for various things.

mca-ctrl -t dump-cfg

renew dhcp interface eth0

show interfaces