Basic PostgreSQL on the Command Line

May 22, 2022

I started working through freeCodeCamp’s beta curriculum for Relational Database. I like to have a contrasting educational opportunity when I need a mental break; solutions to data structure and algorithm problems and other coding questions usually come to me when I’m studying a different topic.

Since databases are my original jam, I thought it would be profitable to work on my command line-level skills so I can gain strength with other types of databases. The first lessons introduce you to working with PostgreSQL to learn syntax. Although I’m very familiar with SQL itself, it had been some time since I had worked with Postgres on the command line. Part of the Flatiron School curriculum had students install Postgres (for me, on my WSL install) but we used SQLite for projects so my knowledge of accessing and using Postgres had gotten fuzzy.

After spending a few days on the CodeAlly platform that is included in freeCodeCamp’s lessons, I decided to try looking back into my local install and get a set of basic instructions together. This will help when I return to my Flatiron projects to convert them to Postgres and deploy them to publicly available servers. For more information on installing Postgres, see

Working with psql on WSL

I knew I had Postgres installed so the first thing I tried was to invoke it. I received an error message.

Check psl

I vaguely recalled that the service is generally stopped so I tracked down the command to get it started.

Start postgres service

I was now able to access psql. I had created an account for myself as part of my Flatiron studies so my account name appears as the prompt.

Open postgres

I didn’t know what databases had been created, so I listed them with \l. There were a lot more than I recalled!

List databases

I connected to one of the databases I recognized from the setup lesson for Flatiron.

Connect to database

I reviewed the objects that were included in this database. The lesson must have been using a blog as the object to model, since one of the objects was a posts table.

Review relations

I further reviewed what the sample lesson asked us to create as fields in the posts table.

Review fields

I connected to a few other relations but found they were empty, so I went ahead and quit Postgres.

Quit Postgres

I also stopped the Postgres service until I needed it again.

Stop Postgres service

Basic commands for psql

  • Check if psql is accessible
  • Start Postgres service
sudo service postgres start
  • List available databases
  • Connect to a specific database to work within it
\c <database_name>
  • Review relations in the database
  • Review details of a specific relation
\d <relation_name>
  • Quit Postgres
  • Stop Postgres service
sudo service postgres stop