Skip to content

Instantly share code, notes, and snippets.

@Auraelius
Last active August 17, 2020 23:11
Show Gist options
  • Save Auraelius/1a9343d19ce652ecb1aa8de23be8925c to your computer and use it in GitHub Desktop.
Save Auraelius/1a9343d19ce652ecb1aa8de23be8925c to your computer and use it in GitHub Desktop.
Notes on learning and using command line interfaces when developing express/prostgres back end software.

Compare GUI vs CLI

You should plan on becoming good at all sorts of user interfaces. Each form of user interface has its advantages and disadvantages. Using only one style of UI is as silly as only using one kind of editor or one language. "Specialization is for insects." - Heinlein

  • CLI - good for
    • low bandwidth environments (talking to cloud servers)
    • very fast interactions for users that already know which command to use
    • can use scripts and the alias command to create powerful custom commands
  • GUI - good for
    • exploring functionality & learning
    • Seeing structure and organization - how things relate to each other

How do I learn a CLI? (Unix/Linux/Mac)

  • Stay local - exhaust your local resources before googling
    • Built-in help
      • pg_ctl --help
      • psql --help
      • psql \q command
    • man pages (Try these in your shell)
      • man man
      • man zsh (or man bash)
      • man pg_ctl
      • man psql
  • Practice reading text that is hard to read
    • You may have to force yourself to keep reading every line when you feel lost.
    • Skim & scan to get an overview, then read for detail
    • This skill also helps with reading error messages
    • This skill also helps with reading code
  • Have a clear idea of what the CLI is supposed to do. (See if you can say a simple statement like "I want to list databases")
  • Keep a notebook in a text file with a selection of command strings and what they do
  • Your goal is not just to get a quick answer. It's to build your brain muscles to work in this text-heavy, jargon-filled environment.

Essential skill - situational awareness: Where am I? Where should I be?

What command line environment am I in?

  • Powershell?
  • gitbash?
  • bash/zsh?
  • psql?
  • text pager?

What commands are expected?

Look at the prompt. Are you in zsh? psql? a text pager? The commands you use depend on the program that you are talking to. Try some form of "help" or "?" or "?" to see what's available.

What folder am I in?

Most of the time, you need to be in a specific folder for best results. It's always better to navigate to the proper folder location in your shell command line and then start up location-dependent GUI tools or running your automation scripts. On the other hand, some tools (like the postgress ones in this course) don't pay attention to their starting location because they connect via special files or network ports.

What's running on my machine right now?

You can use the ps command on OSX/Linux and the Services Manager on Windows.

One common problem is forgetting to stop your express server when you want to debug. For the simple architectures we use in class, you should only have one copy of each type of server running at a time.

Similarly, make sure your database server is running.

NOTE we're having trouble getting postgresql running on windows - when it appears to be running in the Services Manager, it doesn't respond reliably. It's better to disable it in the SM and start it from the command line using pg_ctl.

Usually, there are several ways to do a thing

Which user interface should I use for the command to ...

  • ... create a user role? createuser or psql or dbeaver?
  • ... create a database? createdb or psql or dbeaver?
  • ... run a SQL script? psql or dbeaver?

Try them all before forming habits.

Startup checklist (Start & maintain your own!)

I tend to do something similar to this whenever I start working on express/postgres:

  • Reboot the machine (Really! Just do it. Every day at least.)
  • Log in to your development account (if your normal account is having trouble installing tools, create a fresh account on your laptop)
  • Launch a command line shell
  • In shell: Navigate to your project folder
  • In shell: Launch postgresql database server
  • Launch database client (psql or dbeaver) & verify server is running
  • In shell: Launch vscode (code .)
  • Pick a CLI - your shell or a vscode terminal - and stick with it
  • Run tests (npm test) or server (npm run dev) from that CLI and only that CLI

You should always have these tools available:

  1. postgresql database server
  2. CLI shell in project folder
  3. Database client (psql, dbeaver, or both)
  4. vscode
  5. Postman

Debugging checklist (Start & maintain your own!)

  • Is the postgresql server running? (pg_ctl status)
  • Is only one instance of node/express running? (ps)
  • Is my connection spec correct? (use dbeaver to test connection)
  • Am I in the right CLI? (look at the prompt characters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment