Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`opendb` command - opens the database for a Laravel app in your GUI
opendb () {
[ ! -f .env ] && { echo "No .env file found."; exit 1; }
DB_CONNECTION=$(grep DB_CONNECTION .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_HOST=$(grep DB_HOST .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_PORT=$(grep DB_PORT .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_DATABASE=$(grep DB_DATABASE .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_USERNAME=$(grep DB_USERNAME .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_PASSWORD=$(grep DB_PASSWORD .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"
echo "Opening ${DB_URL}"
open $DB_URL
}
@browner12

This comment has been minimized.

Copy link

@browner12 browner12 commented Sep 17, 2020

one minor suggestion, prepend all the DB_ with a caret, so we ensure it is at the start of the line

DB_CONNECTION=$(grep ^DB_CONNECTION .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)

our .env file sometimes contains more than one database connect, something like LEGACY_DB_CONNECTION, and that fubars this script.

@browner12

This comment has been minimized.

Copy link

@browner12 browner12 commented Sep 17, 2020

also for all you Homestead users out there... hardcode a "0" after the port, so it maps correctly.

DB_URL="${DB_CONNECTION}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}0/${DB_DATABASE}"
@Erth0

This comment has been minimized.

Copy link

@Erth0 Erth0 commented Sep 17, 2020

Anyone can do this for sequel ace ?

@azurinspire

This comment has been minimized.

Copy link

@azurinspire azurinspire commented Sep 18, 2020

If password contains special characters this doesn't work, one workaround is to use PHP's URL encode:

export DB_PASSWORD=$(grep DB_PASSWORD .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_PASSWORD=$(php -r 'echo urlencode(getenv("DB_PASSWORD"));')
DB_URL="postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}"

And for PostgreSQL the Laravel's pgsql in DB_CONNECTION doesn't seem to work in URL, hard coded postgresql://... works

@deanzod

This comment has been minimized.

Copy link

@deanzod deanzod commented Sep 18, 2020

Anyone can do this for sequel ace ?

change last line to this:

   open -a "sequel ace" $DB_URL
@Erth0

This comment has been minimized.

Copy link

@Erth0 Erth0 commented Sep 18, 2020

Anyone can do this for sequel ace ?

change last line to this:

   open -a "sequel ace" $DB_URL

Thanks bro you are a genius <3

@jmcerrejon

This comment has been minimized.

Copy link

@jmcerrejon jmcerrejon commented Sep 21, 2020

Double quote "$DB_URL" to prevent globbing and word splitting. 😉

@amestsantim

This comment has been minimized.

Copy link

@amestsantim amestsantim commented Sep 22, 2020

I was trying to come up with a similar function for launching the given (.env) postgres db in pgAdmin4 but couldn't get anywhere with it.
Has anyone tried that?

@miclf

This comment has been minimized.

Copy link

@miclf miclf commented Sep 22, 2020

The script doesn’t work if the strings in your .env file are wrapped with quotes, but my grep and cut skills aren’t good enough to improve on that.

Nice trick though, didn’t know about mysql://

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment