Data Types

This section will cover common datatypes in PostgreSQL and show us how to determine the data type of a column in an existing table. For detailed list of data types, please visit: https://www.postgresql.org/docs/current/datatype.html

1. Common Data Types

1.1. Character Data Types

  • CHAR (fixed length)

  • VARCHAR (variable-length with limit)

  • TEXT (unlimited length)

1.2. Numeric Data Types

  • INT (4 bytes)

  • DECIMAL(variable)

  • SERIAL(2 bytes)

Numeric Data Type List (Credit: postgresql.org)

1.3. Date/Time Data Types

  • DATE(stores date data. ISO 3601 format standard: 'yyyy-mm-dd'; e.g. '2024-01-01'),

  • TIME(stores time data with microsecond precision with or without time zone, ex '03:15:33.467'),

  • TIMESTAMP(stores date and time data with or without timezone. e.g. '2023-01-21 02:06:11.86123+00' )

  • INTERVAL(stores date and time data as a period of time in years, months, days, hours, seconds, etc. e.g. '7 days')

The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone, and PostgreSQL honors that behavior. timestamptz is accepted as an abbreviation for timestamp with time zone; this is a PostgreSQL extension.

To check the timezones, abbreviations, offsets from the UTC, and whether the timezone follows daylight saving time procedure, you can query PostGRES' pg_timezone_names table.

Example including a week interval:

For more data manipulation examples check out SQL Data Wrangling page.

1.4. Arrays

Much like arrays in typical programming languages, we have the capability to generate multi-dimensional arrays with different lengths for any native data type. To illustrate how to use arrays let us first create a table and populate it:

Note that Array indexing starts at 1 (not 0)

2. Displaying Existing Tables' Columns and Their DataTypes

2.1. Displaying All TABLES existed in a Database

2.2. Displaying All COLUMNs in a Table

2.3. Displaying only the Column Names and the Data Types in a Table

Last updated

Was this helpful?