Created
December 17, 2013 07:06
-
-
Save nihilismus/8001159 to your computer and use it in GitHub Desktop.
shell script for a personal PostgreSQL instance / Slackware Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# About: shell script for a personal PostgreSQL instance / Slackware Linux | |
# Copyright © 2013 Antonio Hernández Blas <hba.nihilismus@gmail.com> | |
# This program is free software. It comes without any warranty, to | |
# the extent permitted by applicable law. You can redistribute it | |
# and/or modify it under the terms of the Do What The Fuck You Want | |
# To Public License, Version 2, as published by Sam Hocevar. See | |
# http://www.wtfpl.net/ for more details. | |
export PGDATA=$HOME/dbms/postgresql | |
export PGPORT=6789 | |
export PGLOG=$HOME/dbms/var/postgresql/postgresql.log | |
export PGSOCKET=$HOME/dbms/var/postgresql | |
export PGDATABASE=postgres | |
export PGHOST=$PGSOCKET | |
export DEFAULTDB=postgres | |
export DEFAULTUSER=$(whoami) | |
export LESSSECURE=1 | |
export PAGER="less --hilite-search --ignore-case \ | |
--status-column --line-numbers --QUIET \ | |
--chop-long-lines --hilite-unread" | |
case $1 in | |
"start") | |
pg_ctl status 2>&1 >/dev/null | |
if [ $? -eq 3 ]; then | |
pg_ctl start -w --log $PGLOG -o "-k $PGSOCKET" | |
exit $? | |
fi | |
pg_dev status | |
exit 1 | |
;; | |
"stop") | |
pg_ctl stop | |
exit $? | |
;; | |
"restart") | |
pg_dev stop | |
sleep 3 | |
pg_dev start | |
;; | |
"status") | |
pg_ctl status | |
exit $? | |
;; | |
"log" | "-l") | |
if [ -n "$2" ]; then | |
tail -n "$2" $PGLOG | |
else | |
tail -f $PGLOG | |
fi | |
exit $? | |
;; | |
"connect" | "-c") | |
if [ -n "$2" ]; then | |
export PGDATABASE=$2 | |
fi | |
psql | |
exit $? | |
;; | |
"interactive" | "-i") | |
psql $DEFAULTDB $DEFAULTUSER | |
exit $? | |
;; | |
"source" | "-f") | |
if [ -z $2 ]; then | |
echo "Error: you must indicate which SQL script." | |
exit 1 | |
fi | |
if [ ! -f "$2" ]; then | |
echo "Error: '$2' does not exist." | |
exit 1 | |
fi | |
psql -f $2 $DEFAULTDB $DEFAULTUSER | |
exit $? | |
;; | |
"url" | "-u") | |
echo "jdbc:postgresql://$PGSOCKET:$PGPORT/" | |
;; | |
*) | |
echo "pg_dev [start | stop | restart | status | log/-l | connect/-c | interactive/-i | source/-f | url/-u]" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment