Creating a Database

Wednesday, 24 Oct 2018 Kudzai Nyandoro

A database is a collection of information that is electronically stored in a particular way for easy access. PostgreSQL is a database management system (DBMS), a tool used for retrieving information from the database using Structured Query Language (SQL). PostgreSQL is a powerful open-source object-relational database used by small and large companies such as Instagram, Netflix, Uber, Pandora, Spotify, Groupon etc.

You can read more on open-source here. In this tutorial, we're going to warm up to SQL and PostgreSQL by learning how to create a database, enter into the PostgreSQL console, delete the database etc.

This tutorial assumes that you have already read the "Introductory to SQL" and "PostgreSQL Database" blogs . Also check out Episode #7, #8 and #9 if you do not already have PostgreSQL and Git installed on your computer.

As usual, the first step is to fire up your terminal. If you prefer to use a graphical user interface (GUI), you can install pgAdmin. Installing pgAdmin is outside the scope of this tutorial because it is easy to install and there is great documentation online on how to install it.

I've noticed that the terminal really does a good job at helping me build up on my muscle memory. After a little practice, many commands become a reflex action for me. Okay, the real truth is that l can't remember all this stuff and the terminal does the trick! It's like -- magic :-) Take your pick GUI or Terminal, they all work and what you choose is a matter of preference.

To better follow along with any of my tutorials and stay on track, I strongly suggest sticking with the terminal. If you choose to use pgAdmin and need help getting started, I'll briefly show you how to use this tool in one of the upcoming tutorials. All Commands that I run in the terminal can also be run in PgAdmin.

I will follow this up with the screencast coming soon. You can give the database any name of your choice. I'm going to start with rivers because I'm a water baby from the Zambezi and I love rivers.

Create a database named rivers, notice you're not yet in the PostgreSQL console. Hint ... hint $

$ createdb rivers

Enter into the default database

$ psql 

Delete a database named rivers, notice that you're now in the Postgres console. Hint ... hint postgres#=

postgres=# DROP DATABASE rivers;

Create a database named rivers. Compare the syntax used in step one with the one below.

postgres=# CREATE DATABASE rivers;

Connect to the rivers database

postgres=# \c rivers

List the databases

rivers=# \l


=# \q