dbx

How To Create A Table In PostgreSQL

By dbx Team on 2024-09-02

What Is A Table?

A table is a collection of data organized in a structured format. In PostgreSQL, tables are used to store data in a relational database. Each table consists of rows and columns.

How Do You Create A Table in PostgreSQL?

The following SQL statement creates a table named street in your PostgreSQL database:

CREATE TABLE public.street 
(id bigint primary key generated always as identity,
  name text,
  city text,
  description text)

This enables you to create a table named street that holds information about streets. Each street has an automatically generated unique id, a name, a city, and a description. The id serves as the primary key, ensuring that each entry is uniquely identifiable.

Display Table

You can "display" the empty table you just created with another SQL statement:

SELECT * FROM public.street;

Which will give you this result:

Understanding your Table

What are the names and data types of the columns in the street table in the public schema?

SELECT column_name, data_type 
FROM information_schema.columns 
WHERE table_schema = 'public' 
  AND table_name = 'street';

This query retrieves the names and data types of columns in the street table from the database's metadata. It pulls the column information from the information_schema.columns view and filters the results to include only columns from the street table in the public schema.