My Postgres environment

· 433 words · 3 minute read

Last week, I gave my “psql is awesome” conf talk for the last time. Each time I explain how I love working with CLI and why having inputs and outputs in text format is great, some people come to ask me to explain how I use my terminal. So I think it’s time to explain how I work.

Tmux 🔗

First, I work with tmux as a terminal multiplexer. Tmux is so integrated with my terminal that I have added those lines into my .bashrc file to always start the terminal inside tmux. And before you say it, yes, I know I should switch to zsh, it’s on my todo.

# Tmux by default
if command -v tmux &> /dev/null && [ -n "$PS1" ] && [[ ! "$TERM" =~ screen ]] && [[ ! "$TERM" =~ tmux ]] && [ -z "$TMUX" ]; then
     exec tmux
fi

My .tmux.conf file 🔗

I customize my tmux configuration like this:

# window management {{{
unbind %
unbind \"
unbind s
bind | split-window -h
bind _ split-window -v
bind-key S command-prompt "attach-session -t '%%'"
bind-key s choose-session
# }}}

# Vi style bindings {{{
setw -g mode-keys vi
unbind Escape

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind-key Y run "tmux save-buffer - | xclip -i -selection clipboard &> /dev/null"
# }}}

# Resizing {{{
bind < resize-pane -L 5
bind > resize-pane -R 5
bind - resize-pane -D 5
bind + resize-pane -U 5
# }}}

# bind reload key
bind-key r source-file ~/.tmux.conf \; display-message "Config reloaded!"

# More history!
set -g history-limit 50000

Normally the comments should explain what they do. If you don’t know tmux, you’ll find great beginner’s guides on the internet.

The tmux-logging plugin 🔗

On top of tmux, I use a tmux plugin called tmux logging (available here). This plugin will allow you to start logging from the current pane to a text file. Each of my inputs and outputs will go into that file. If I forgot to trigger the plugin history from the beginning, I can trigger it afterward to save the whole history (hence my history setting to have more lines in memory).

Using psql 🔗

Normally, when I want to write some SQL query, I divide my terminal screen into two panes vertically, one with psql, so that I can query some data (column names, SQL syntax…) and one with vim to write down my query. I just need to use the \i <filename> command to execute my query.

Maybe it will be more clear with an image:

My tmux setup