Yak Shaving My Way To Hacktoberfest - Day 2
Tags: hacktoberfest, yakshaving, shell, postgres
Published: 2019-10-19 09:56:05

---
title: Yak Shaving My Way To Hacktoberfest - Day 2
published: true
description: On how an OS upgrade can kill your Terminal
tags: hacktoberfest, yakshaving, shell, postgres 
series: Yak Shaving My Way To Hacktoberfest
---

#### _On how an OS upgrade can kill your Terminal_

I woke up to the upgrade to Catalina being completed. I had a couple of hours after work I could use to fix the setup.

I launched [iTerm](https://www.iterm2.com/features.html) that with a very graceful popup tells me something-something command profile. As soon as I acknowledged the popup, the window closed. I tried another couple of times (monkey monkey), decided to close permanently the popup because brainfart. Probably I was hoping to be able to see the content of the iTerm window underneath. Unable to revert the behavior, I realized I have two profiles on iTerm. I opened the default profile and there it was, the popup in its splendor.

```
A session ended very soon after starting. Check that the command in profile “molli” is correct.
```

Looking for the message online [told me](https://apple.stackexchange.com/questions/344971/two-red-lines-bars-in-iterm2-window-and-error-message-about-session-ending-what) that something was wrong with the command that iTerm launches at start and that I should look at what the window behind the popup says. I couldn't move the popup around so I had to revert to reopen the window a few times and be reading very fast 🙄

It seemed that bash was causing the problem. I went to my `bash_profile` and commented out everything bash-related. Relaunching iTerm showed the same issue. I suddenly remembered that, with the last version of MacOS, Apple [started suggesting](https://insights.dice.com/2019/06/12/macos-catalina-zsh-bash-xcode-11/) devs to [switch to `zsh`](https://www.theverge.com/2019/6/4/18651872/apple-macos-catalina-zsh-bash-shell-replacement-features). I fired the OS terminal app that presented the same problem: I cannot use it because it's still looking for bash.

![but-why.gif](https://2.bp.blogspot.com/-3xtPQ5Mc88c/VTiUiYJspSI/AAAAAAAAS-c/rF-0oHwlb4A/s1600/but-why.gif)

In my skimming through internet pages and iTerm settings, I realized there was a *Command* setting under Profiles > General that was set to *Login Shell*. Finally a stable iTerm with, [guess what](http://osxdaily.com/2018/12/29/use-zsh-default-terminal-mac-os-x/)? 

```
/bin/zsh/
```

Of course it wasn't enough, because to my subsequent `brew doctor`, the terminal replied with the equivalent of "I have no clue what you are talking about". But I knew the little rascal (...) was there. FINE. Maybe it was something in the path that zsh doesn't pick up. I tried to configure it properly, but this meant understanding the [difference](https://superuser.com/a/187673) between `.zprofile`, `.profile` and `.zshrc`.

I ended up with a `.zprofile` that looks like this

```
emulate sh
. ~/.profile
emulate zsh
export PATH="/usr/local/sbin:$PATH"
export PATH=/usr/local/bin:$PATH
```

but still no joy for my brewing.

I checked Terminal and lo and behold, it did indeed recognize homebrew. So I used it to launch `homebrew` and installed `bash` (sorry, zsh, we'll meet again soon).

I reverted iTerm to use bash too, but it would still close. I made sure I had the latest version of batch available on homebrew, but [when I issue `echo $SHELL`](http://osxdaily.com/2009/09/25/what-shell-am-i-using/), what I get is still bash 4.3.0. I couldn't figure out where it'd pick this up, because I was 100% sure at this point I had no version named anywhere.

Apparently, on Mac, [you can set your `PATH` variable in multiple places](https://www.pegaxchange.com/2017/06/17/mac-os-x-path-variable/), one of them will add an entry *permanently for all users on the machine*.

I dug my `/etc/shell`, changed the config, iTerm doesn't close anymore.

```
source ~/.bash_profile
-bash: __git_ps1: command not found
```

![FFUU.jpg](https://rlv.zcache.com/rage_guy_angry_fuu_fuuu_rage_face_meme_poster-r2505f4c7185e472c9b975195048c681d_a21y_8byvr_307.jpg)

It was 9.15AM. I had a meeting at 10. I was still in my pijiama. 

Well...

On my way to the subway, I realized I should save this knowledge somewhere. I was learning stuff. I should have a blog. I should actually have a self-hosted blog, because I would be sure that then the content would belong to me 100%. 

I stopped. That would have added a WHOLE new yak to shave. What is the mantra of the agile ~daleks~ software engineers? Iterate! ITERATE!
So I went for the fastest thing I could get my hands on and I opened my DEV.TO profile. On my phone, I hacked the first lines of the first post of this series. 

Fast forward to 7PM. I was finally home and decided to finish the first post. I had to search for all the sources and reconstruct in my memory what happened the day before. I also decided to note down what happened on that day (you don't want to see those notes, but as you can see, it was a good call, after a week I would have ZERO recollection of any of this).

Now back to my last problem of the day, `__git_ps1`.

I have my prompt configured in such a way that, when I am in a folder that is a git repository, it shows me the status of the files and the branch I am in. Since I had an old version of git and an old version of OSX, the former configuration wouldn't work. I updated `.bash_profile` to

```
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
export PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[01;33m\]\$(__git_ps1)\[\033[01;34m\] \$\[\033[00m\] "
```

as [described here](https://stackoverflow.com/a/15253752, update bash_profile) and I had my git status on my prompt again, all cool.

I ran `rake db:setup` only to be told that - of course - the database server wasn't running. Because I'm lazy, I have an alias for that too

```
alias pgstart='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
```

so I added it to `.bash_profile` and, when sourcing it, bash very nicely told me

```
-bash: pg_ctl: command not found
```

I launched and initialized the Postgres app, same outcome. RTFM, Monica: on [https://postgresapp.com/](https://postgresapp.com/) they clearly say to add the Postgres app to the path permanently (remember?) with

```
sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
```

This was the first time I was seeing `tee`, a [command used](https://www.computerhope.com/unix/utee.htm) to write to two different places at the same time (by default, to a file and to standard output).
But I questioned the permanent add to the path, so I added the postgres app location to the user `PATH` in `.bash_profile` and sourced it to pick up the changes.

An `echo $PATH` showed me that the changes are there, but the PATH itself was ever-growing and also full of duplicates because of the multiple `source`s. How could I get rid of the duplicates?

There are a bunch of suggestions out there on how to [clean](https://unix.stackexchange.com/questions/40749/remove-duplicate-path-entries-with-awk-command) and [reset](https://superuser.com/questions/121870/how-do-i-reset-the-path-variable-on-mac-os-x) PATH or [avoid duplication](https://unix.stackexchange.com/questions/4965/keep-duplicates-out-of-path-on-source) [completely]((https://unix.stackexchange.com/questions/14895/duplicate-entries-in-path-a-problem)), but it would take too long to do it properly, I was still far away from being able to start coding. So I decided to chop everything off, reset PATH to what I believed was a clean slate and sourced again (MVPs, right?!)

```
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
source ~/.bash_profile
```

gave me back something sensible and clean. Noice.

Looking at my `.bash_profile` configuration for environment managers, I noticed and commented out some nonsense shit about `rbenv` that is not relevant and I realized that I didn't have [`exenv`](https://github.com/mururu/exenv) working anymore. But [`asdf`](https://asdf-vm.com/#/core-manage-asdf-vm) did indeed work, so I removed the first one permanently.

With my brand new and shiny PATH, I could now try and call `pgstart` again. The command run but it couldn't start the server. I looked at the logs

```
2019-10-09 07:43:43.634 CEST [37155] FATAL:  database files are incompatible with server
2019-10-09 07:43:43.634 CEST [37155] DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 12.0.
2019-10-09 07:54:17.196 CEST [49284] FATAL:  database files are incompatible with server
2019-10-09 07:54:17.196 CEST [49284] DETAIL:  The data directory was initialized by PostgreSQL version 9.4, which is not compatible with this version 12.0.
```

O. K. 

I removed current installation, reinstalled everything with homebrew, because it was the fastest option. I could then [also upgrade](https://formulae.brew.sh/formula/postgresql) the data with homebrew.

```
 brew postgresql-upgrade-database
```

A pgstart later, the terminal still tried to start `pg_ctl` from the Postegre.app installation directory. I needed to check where the [aliases](https://unix.stackexchange.com/a/544970) where defined and which where currently available on my machine. I cleaned the aliases and resourced again the profile for bash, now `which pg_ctl` gave me the correct directory, I can start the postgres server.

The moment of truth, running `rake db:setup` again.


```
ActiveRecord::NoDatabaseError: FATAL:  role "postgres" does not exist
```

It was an error, but I had never been happier to see one. I dug again how to [create a role](https://www.digitalocean.com/community/tutorials/how-to-use-roles-and-manage-grant-permissions-in-postgresql-on-a-vps--2) within postgres. 

I issued `psql` to check the current roles and users available, but

```
psql: FATAL:  database "molli" does not exist
```

I proceeded to [create the db from the command line](https://tomcam.github.io/postgres/#creating-a-database).

I had no idea which password my `molli` user has, so I proceeded to look for [how to reset user password](https://stackoverflow.com/a/28643961) in postgres and set a new one.

I finally was able to create the role for postgres with the same privileges as my main one.

`db:setup` works! `db:test:prepare` works!

8:50AM, time to go to work... I might finally be able to code.