Introduction To Relational Databases Using Postgres
Create Table in Postgres
S Soumen
Oct 3, 2023

Understanding Various Data Types in PostgreSQL

PostgreSQL supports a wide range of data types. Here, we'll cover some of the most commonly used types: Numeric Types 

Integer: Used for whole numbers. Variants include    - smallint: 2 bytes    - integer: 4 bytes    - bigint: 8 bytes

Serial: Auto-incrementing integer, useful for primary keys. smallserial, serial, bigserial

Numeric: For exact, user-specified precision numbers. Useful for monetary amounts where precision is crucial. Real and Double Precision: Floating point numbers for scientific calculations.

Character Types CHAR(n): Fixed-length character string. Space-padded on the right to the specified length n. VARCHAR(n): Variable-length character string with a limit of n characters. TEXT: Variable-length character string with unlimited length.

Date/Time Types date: For date values (year, month, day). time: Time of day (without time zone). timestamp: Date and time (without time zone). timestamptz: Timestamp with time zone.

Creating a Table in PostgreSQL: Example

Example PSQL Query:

CREATE TABLE student(
    roll_number    int,
    first_name     VARCHAR(50),
    last_name      VARCHAR(50),
    gender         VARCHAR(20),
    date_of_birth  date
);

Have a doubt?
Post it here, our mentors will help you out.