Episode #14 | Creating Our First SQL Table


Wednesday, 31 Oct 2018 Kudzai Nyandoro 9:55 Minutes

Description:

In this tutorial, we're going to learn how to create a table using SQL and PostgreSQL.

Notes:

Creating a Table

To create a table, first, start your PostgreSQL server by running one of the commands below, depending on the machine you're using.

If you're on AWS Cloud9: Start your PostgreSQL server if you've not yet started it.

$ sudo service postgresql start

If you're on a MacOS: Start your PostgreSQL server if you've not yet started it.

$ brew services start postgresql

If you're on an Ubuntu OS: Start your PostgreSQL server if you've not yet started it.

$ service postgresql start

Enter into the PostgreSQL database

$ psql postgres

View a list of all your databases

postgres=# \l

Connect to the rivers database

$ postgres=# \c rivers

Create a table for California Rivers

-- Create a table for California rivers
CREATE TABLE california_rivers(
  river_id          SERIAL PRIMARY KEY,
  name              VARCHAR(50),
  ideal_flow        INTEGER
);

Our First SQL Query

rivers=# SELECT * FROM california_rivers;
 river_id | name | ideal_flow
----------+------+-------+---------+--------+----------+
(0 rows)

You can also learn more about creating tables here. Source code on GitHub.