In this section, we will learn how to manage tables in the database.
PostgreSQL has three types of tables:
- Temporary tables: Very fast tables, visible only to the user who created them
- Unlogged tables: Very fast tables to be used as support tables common to all users
- Logged tables: Regular tables
We will now use the following steps to create a user table from scratch:
- Create a new database using the following command:
forumdb=# create database forumdb2;
CREATE DATABASE
- Execute the following command:
forumdb=# \c forumdb2
You are now connected to database "forumdb2" as user "postgres".
forumdb2=# CREATE TABLE users (
pk int GENERATED ALWAYS AS IDENTITY
, username text NOT NULL
, gecos text
, email text NOT NULL
, PRIMARY KEY( pk )
, UNIQUE ( username )
);
CREATE TABLE
The CREATE TABLE command creates a new table. The command GENERATED AS IDENTITY, automatically assigns a unique value to a column.
- Observe what was created on the database using the...