-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg-passwd
executable file
·70 lines (61 loc) · 1.53 KB
/
pg-passwd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/sh
set -e
set -u
fn_help() { (
echo "pg-passwd v1.0.0 - reset a user's password"
echo ""
echo "USAGE"
echo " pg-passwd <user> [port]"
echo ""
echo "EXAMPLE"
echo " pg-passwd foobar-xxxxxx 5432"
echo ""
echo "COPYING"
echo " Copyright (c) 2024 AJ ONeal <[email protected]>"
echo " Licensed under the MPL-2.0"
); }
fn_pg_set_password() { (
b_user="${1}"
b_pw="${2}"
b_port="${3}"
echo "Resetting password for user '${b_user}' ..."
echo "ALTER USER \"${b_user}\" WITH PASSWORD '${b_pw}';" |
psql "postgres://postgres:postgres@localhost:${b_port}/postgres" -f -
); }
fn_rnd_base58() { (
xxd -c 0 -l 64 -p /dev/urandom |
xxd -r -ps |
base64 -w 0 |
tr -d /+_=- |
tr -d 0IOl |
cut -c 1-22
); }
main() { (
case ${1:-} in
--help | help)
fn_help
return 0
;;
-V | --version | version)
fn_help
return 0
;;
"")
fn_help >&2
return 1
;;
*) ;;
esac
b_pguser="${1:-}"
b_pgport="${2:-5432}"
b_pw_base58="$(fn_rnd_base58)"
fn_pg_set_password "${b_pguser}" "${b_pw_base58}" "${b_pgport}" >&2
echo >&2 ""
echo "password: '${b_pw_base58}'"
echo >&2 ""
echo "pgpass: localhost:${b_pgport}:${b_pguser}:${b_pguser}:${b_pw_base58}"
echo >&2 ""
echo "psql 'postgres://${b_pguser}:${b_pw_base58}@localhost:${b_pgport}/${b_pguser}'"
echo >&2 ""
); }
main "${@:-}"