🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to PostgreSQL Notes
Topic #6

PostgreSQL CREATE TABLE


Connect to the Database

To create a new database table using the SQL Shell, make sure you are connected to the database. If not, follow the steps in the Get Started chapter of this tutorial.

Once you are connected, you are ready to write SQL statements!


Create Table

The following SQL statement will create a table named cars in your PostgreSQL database:

CREATE TABLE cars (
  brand
VARCHAR(255),
  model
VARCHAR(255),
  year INT
);

When you execute the above statement, an empty table named cars will be created, and the SQL Shell application will return the following:

CREATE TABLE

In the SQL Shell application on your computer the operation above might look like this:

image


SQL Statement Explained

The above SQL statement created an empty table with three fields: brand, model, and year.

When creating fields in a table we have to specify the data type of each field.

For brand and model we are expecting string values, and string values are specified with the VARCHAR keyword.

We also have to specify the number of characters allowed in a string field, and since we do not know exactly, we just set it to 255.

For year we are expecting integer values (numbers without decimals), and integer values are specified with the INT keyword.


Display Table

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

SELECT * FROM cars;

Which will give you this result:

 brand | model | year
-------+-------+------
(0 rows)

In the SQL Shell application on your computer the operation above might look like this:

image

In the next chapters we will learn how to insert data into a table, and also more on how to retrieve data from a table.

Want to go beyond the notes?

Join CodingNow 2.0's PostgreSQL course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

PostgreSQL CREATE TABLE – FAQs

Quick answers about learning PostgreSQL CREATE TABLE in PostgreSQL.

This free note from CodingNow 2.0 explains PostgreSQL CREATE TABLE in PostgreSQL — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every PostgreSQL topic on CodingNow 2.0, including PostgreSQL CREATE TABLE, is 100% free with no signup required.
With focused practice, most students grasp PostgreSQL CREATE TABLE in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now