SQL data types cheat sheet
Integers
-
INT / integer
4 bytes- MySQL and MariaDB
- INT (or INTEGER)
- PostgreSQL
- integer
- Range
- -2,147,483,648 to 2,147,483,647
- UNSIGNED
- 0 to 4,294,967,295
The most common integer type, suited to quantities, counters, and IDs in tables of normal size. The PostgreSQL documentation notes that integer offers the best balance between range, storage size, and performance.
-
BIGINT / bigint
8 bytes- MySQL and MariaDB
- BIGINT
- PostgreSQL
- bigint
- Range
- -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- UNSIGNED
- 0 to 18,446,744,073,709,551,615
A large integer, for IDs in tables that may outgrow the INT range, such as orders, logs, and events tables. Changing the type of a key column in a large table is a heavy operation, so choose BIGINT when you design the table if you expect it to grow.
-
SMALLINT / smallint
2 bytes- MySQL and MariaDB
- SMALLINT
- PostgreSQL
- smallint
- Range
- -32,768 to 32,767
- UNSIGNED
- 0 to 65,535
A small integer, for values with a known, limited range, such as a year, a floor number, or a status code. In PostgreSQL, this is the smallest integer type.
-
TINYINT
1 byte- MySQL and MariaDB
- TINYINT
- PostgreSQL
- No equivalent type; use smallint
- Range
- -128 to 127
- UNSIGNED
- 0 to 255
The smallest integer in MySQL and MariaDB, for flags and very small values. BOOL and BOOLEAN are synonyms for TINYINT(1).
-
MEDIUMINT
3 bytes- MySQL and MariaDB
- MEDIUMINT
- PostgreSQL
- No equivalent type; use integer
- Range
- -8,388,608 to 8,388,607
- UNSIGNED
- 0 to 16,777,215
An intermediate type that exists only in MySQL and MariaDB, between SMALLINT and INT. When you move to PostgreSQL, you convert it to integer.
-
UNSIGNED
Unsigned- MySQL and MariaDB
- An attribute of integer types that shifts the whole range to nonnegative values
- PostgreSQL
- No unsigned types; restrict the values with CHECK (col >= 0)
A column with UNSIGNED accepts only positive values and zero, and its upper limit roughly doubles. In MySQL, the UNSIGNED attribute for DECIMAL, FLOAT, and DOUBLE is deprecated, and the documentation recommends a CHECK constraint instead. When you move to PostgreSQL, an INT UNSIGNED column that may exceed 2,147,483,647 is converted to bigint.
-
INT(11)
Display width- MySQL and MariaDB
- The number in parentheses is only a display width and doesn't limit the range
- PostgreSQL
- No display width; write integer
INT(11) stores exactly the same range as INT. In MySQL, the display width of integer types is deprecated, and so is the ZEROFILL attribute. TINYINT(1) is still common, because BOOL and BOOLEAN are synonyms for it.
Auto-increment and IDs
-
AUTO_INCREMENT
Auto-increment- MySQL and MariaDB
- id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
- PostgreSQL
- id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY
A column that automatically gets an increasing number for each new row, and the common way to create a primary key. In MySQL and MariaDB, you add the AUTO_INCREMENT attribute to the column, and in PostgreSQL, you define an identity column, as the SQL standard specifies.
-
GENERATED AS IDENTITY
PostgreSQL- ALWAYS
- A manual value in INSERT is accepted only with OVERRIDING SYSTEM VALUE
- BY DEFAULT
- A manual value in INSERT takes precedence over the automatic numbering
- MySQL and MariaDB
- AUTO_INCREMENT
An identity column gets its own sequence and is automatically NOT NULL. The ALWAYS option prevents inserting and updating manual values by mistake, and the BY DEFAULT option is convenient when you migrate existing data with its IDs.
-
SERIAL / serial
Shorthand- MySQL and MariaDB
- SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
- PostgreSQL
- smallserial, serial, and bigserial: an integer with a sequence, without an automatic unique index
SERIAL means different things in the two systems. In PostgreSQL, the serial types aren't true types, but a shorthand that creates an integer column, a sequence, and a default value from the sequence. A UNIQUE or PRIMARY KEY constraint isn't added automatically, so you add it yourself. For new tables in PostgreSQL, you usually use an identity column.
-
UUID / uuid
128 bits- MySQL
- BINARY(16) with UUID_TO_BIN() and BIN_TO_UUID(), or CHAR(36) as text
- MariaDB
- Recent versions have a dedicated UUID type
- PostgreSQL
- uuid, with gen_random_uuid() or uuidv7()
A 128-bit unique identifier that's easy to generate outside the database as well. In PostgreSQL, gen_random_uuid() generates a random version 4 UUID, and in recent versions, uuidv7() generates a time-ordered version 7 UUID that's better suited to indexes. In MySQL, storing it as BINARY(16) saves space compared with CHAR(36), and UUID_TO_BIN with the flag 1 makes the index more efficient for values from UUID().
Decimal numbers
-
DECIMAL / numeric
Exact- MySQL and MariaDB
- DECIMAL(M,D) or NUMERIC, up to 65 digits, up to 30 of them after the decimal point
- PostgreSQL
- numeric(p,s) or decimal, with a declared precision of up to 1000 digits
- Suitable for
- Money amounts, prices, balances, and any value that needs full precision
An exact decimal number, with no rounding errors. M or p is the total number of digits, and D or s is the number of digits after the decimal point, so DECIMAL(12,2) stores up to 10 digits before the point and 2 after it. In MySQL, when you don't specify the precision, the default is DECIMAL(10,0), with no digits after the point.
-
FLOAT / real
4 bytes, approximate- MySQL and MariaDB
- FLOAT
- PostgreSQL
- real, with a precision of about 6 digits
- Not suitable for
- Money amounts and exact comparison of values
A single-precision floating-point number. The value is stored as an approximation, so calculations and comparisons may return a result slightly different from what you expect. Suited to measurements and scientific calculations, not to money.
-
DOUBLE / double precision
8 bytes, approximate- MySQL and MariaDB
- DOUBLE
- PostgreSQL
- double precision, with a precision of about 15 digits
- Not suitable for
- Money amounts
A double-precision floating-point number, with a larger range and precision than FLOAT, but still approximate. The PostgreSQL documentation points to the numeric type wherever exact storage and calculation are required, such as money amounts.
-
money
PostgreSQL- PostgreSQL
- money, 8 bytes, displayed according to the lc_monetary currency setting
- MySQL and MariaDB
- No such type; use DECIMAL
The precision and display of money depend on the lc_monetary setting of the database, so restoring a backup into a database with a different setting may fail. For money amounts, you usually use numeric, and keep the currency code in a separate column when there are several currencies.
Boolean values
-
BOOLEAN / boolean
True or false- MySQL and MariaDB
- BOOL and BOOLEAN are synonyms for TINYINT(1); zero is false and any other value is true
- PostgreSQL
- boolean, 1 byte, with the values true, false, and NULL
In MySQL and MariaDB, TRUE and FALSE are aliases for 1 and 0, and the column can also accept other numbers. In PostgreSQL, boolean accepts input such as true, yes, on, or 1 for true, and false, no, off, or 0 for false, and displays the result as t or f.
Strings
-
CHAR / char(n)
Fixed length- MySQL and MariaDB
- CHAR(n), 0 to 255 characters long
- PostgreSQL
- char(n) or character(n)
A fixed-length string that pads short values with spaces. In MySQL, trailing spaces are removed when the value is retrieved. The PostgreSQL documentation notes that char(n) is usually the slowest of the string types because of its additional storage, so use it only for values that truly have a fixed length, such as a country code.
-
VARCHAR / varchar(n)
Variable length- MySQL and MariaDB
- VARCHAR(n), up to 65,535 by definition, subject to the row size and the character set
- PostgreSQL
- varchar(n), and without n it accepts a string of any length
A variable-length string with a maximum length, for names, email addresses, and titles. In MySQL, all the columns in a row share a limit of 65,535 bytes, and in utf8mb4 each character takes up to 4 bytes, so the practical length is smaller. In PostgreSQL, there's no performance difference between varchar(n) and text, and n serves only as a length limit.
-
TEXT / text
Long text- MySQL and MariaDB
- TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT, up to 255 bytes, 64 KB, 16 MB, and 4 GB
- PostgreSQL
- text, with no declared length limit, up to about 1 GB per value
For long content such as articles, descriptions, and notes. In MySQL, the four TEXT types differ only in their maximum length, and an index on a TEXT column requires a prefix length, for example INDEX (body(100)). In PostgreSQL, text is the native string type of the system, and you use it in most cases.
-
ENUM
List of values- MySQL and MariaDB
- ENUM('small','medium','large'), up to 65,535 values
- PostgreSQL
- CREATE TYPE size AS ENUM (...), or text with a CHECK constraint
A column that accepts only one value from a fixed list. In MySQL, the values are stored as numbers, and sorting follows the order of the list, not alphabetical order. In PostgreSQL, an ENUM type is created once and used by several tables; you can add values to it, but you can't remove an existing value. When the list changes often, a separate lookup table is more flexible.
-
SET
MySQL and MariaDB- MySQL and MariaDB
- SET('a','b','c'), several values from a list, up to 64 members
- PostgreSQL
- No such type; use an array, such as text[], or a separate table
A column that can hold several values from a fixed list at once, such as days of the week or permissions. When you need to search by a specific value or change the list, a separate link table is easier to maintain.
Character sets and collations
-
utf8mb4
Character set- MySQL
- utf8mb4, up to 4 bytes per character; utf8mb3 is deprecated, and utf8 is an alias for it
- MariaDB
- By default, utf8 is an alias for utf8mb3; for all characters, choose utf8mb4
- PostgreSQL
- UTF8 encoding, up to 4 bytes per character, set for each database when it's created
utf8mb3 stores up to 3 bytes per character, so it doesn't support emoji and other supplementary characters outside the basic Unicode range. The MySQL documentation recommends utf8mb4 for every new application. In PostgreSQL, you set the encoding in CREATE DATABASE with the ENCODING option.
-
Collation
Sorting and comparison- MySQL and MariaDB
- Each character set has several collations, set for the server, database, table, or column
- PostgreSQL
- LC_COLLATE and LC_CTYPE are set for each database when it's created, and you can also set COLLATE for a column
A collation determines how strings are sorted and compared, for example whether uppercase and lowercase letters are treated as different. Columns you join in queries should therefore use the same character set and the same collation; otherwise, comparison errors or slowdowns may occur.
Binary data
-
BLOB / bytea
Raw bytes- MySQL and MariaDB
- TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB, up to 255 bytes, 64 KB, 16 MB, and 4 GB
- PostgreSQL
- bytea, displayed by default in hex format starting with \x
For binary data such as small files, keys, and hashes. Unlike TEXT, BLOB values are stored as bytes with no character set, and sorting and comparison are based on the byte values. Large files, such as images and videos, are usually stored as files on the server, and the database stores only their path.
Dates and times
-
DATE / date
Date only- MySQL and MariaDB
- DATE, from 1000-01-01 to 9999-12-31
- PostgreSQL
- date, 4 bytes, from 4713 BC to the year 5874897
A date without a time, for birth dates, expiration dates, and holidays. The standard format in both systems is YYYY-MM-DD.
-
TIME / time
Time- MySQL and MariaDB
- TIME, from -838:59:59 to 838:59:59
- PostgreSQL
- time, from 00:00:00 to 24:00:00
In MySQL, TIME is used both for a time of day and for a time interval, so it can exceed 24 hours and can even be negative. In PostgreSQL, time is a time of day only, and you use interval for time intervals.
-
DATETIME
No time zone conversion- MySQL and MariaDB
- DATETIME, from 1000-01-01 00:00:00 to 9999-12-31 23:59:59
- PostgreSQL
- timestamp (without time zone)
A date and time stored exactly as entered, with no conversion between time zones. Suited to dates far in the future and to times that don't depend on location. When the application works across several time zones, store the values in UTC and convert them for display.
-
TIMESTAMP
MySQL and MariaDB- MySQL
- From 1970-01-01 00:00:01 to 2038-01-19 03:14:07 UTC
- MariaDB
- In recent versions, the range extends to 2106-02-07 06:28:15 UTC
- PostgreSQL
- timestamptz
A TIMESTAMP value is converted from the time zone of the connection to UTC when it's stored, and back when it's retrieved, so a connection with a different time zone gets a different time. Because of the 2038 limit, dates far in the future, such as the expiration of a long subscription, are stored in DATETIME.
-
timestamp / timestamptz
PostgreSQL- timestamp
- Without time zone; a time zone in the input is ignored
- timestamptz
- Stored in UTC and displayed according to the TimeZone setting of the connection
- Range
- From 4713 BC to the year 294276, with microsecond precision
timestamptz is short for timestamp with time zone. Despite the name, the original time zone isn't stored: the value is converted to UTC and displayed in the time zone of the connection. For moments in time, such as when an order was placed or a user logged in, you usually use timestamptz.
-
interval
Time interval- PostgreSQL
- interval, 16 bytes, for example interval '1 day 2 hours'
- MySQL and MariaDB
- No such column type; TIME stores up to 838 hours
A time interval in years, months, days, and hours that you can add to or subtract from a date, for example now() + interval '30 days'. In MySQL, you can use the INTERVAL expression in date calculations, but not as a column type.
-
Fractional seconds
Precision- MySQL and MariaDB
- DATETIME(6), TIMESTAMP(6), and TIME(6), down to the microsecond
- PostgreSQL
- timestamp(p) and time(p), down to the microsecond
The number in parentheses sets how many digits after the decimal point are stored for the seconds, from 0 to 6. In MySQL, without a number in parentheses, fractional seconds aren't stored, and higher precision adds up to 3 bytes to each value.
JSON
-
JSON / jsonb
JSON documents- MySQL
- JSON, with validation and an internal format for fast reads
- MariaDB
- JSON is an alias for LONGTEXT with an automatic JSON_VALID check
- PostgreSQL
- json stores the text as is; jsonb stores it in a binary format
Both systems reject an invalid JSON document. In MySQL, you can't index a JSON column directly, so you index a generated column that extracts a value from it. In PostgreSQL, jsonb is faster to process, supports GIN indexes, and removes duplicate keys, and the documentation recommends it for most applications.
Special types
-
Arrays
PostgreSQL- PostgreSQL
- integer[], text[], with '{1,2,3}' or ARRAY[1,2,3]
- MySQL and MariaDB
- No array type; use JSON or a separate table
In PostgreSQL, any type can be an array, and numbering starts at 1, so tags[1] is the first element. The documentation notes that frequently searching for an element in an array may be a sign of poor database design, and that in that case a separate table with a row for each value is a better fit.
-
inet / cidr
IP addresses- PostgreSQL
- inet for an address or a network, cidr for a network only, macaddr for a MAC address
- MariaDB
- INET4 and INET6
- MySQL
- No dedicated type
Dedicated types check that the address is valid and support operations such as checking whether an address belongs to a network. The difference in PostgreSQL: inet accepts 192.168.0.1/24, while cidr rejects a value with bits set to the right of the netmask and accepts only 192.168.0.0/24. In MariaDB, INET6 also stores IPv4 addresses in the form ::ffff:192.0.2.128.
-
Spatial data
Geometry- MySQL and MariaDB
- GEOMETRY, POINT, LINESTRING, POLYGON, and the MULTI types
- PostgreSQL
- point, line, lseg, box, path, polygon, and circle
Types for points, lines, and areas, such as the location of a branch or a delivery zone. In MySQL and MariaDB, GEOMETRY can hold any of the other types. In PostgreSQL, the built-in types describe geometric shapes on a plane.
No matching data types.
How to choose a data type
Money amounts: Use DECIMAL or numeric with a fixed number of digits after the decimal point, for example DECIMAL(12,2). FLOAT and DOUBLE store an approximate value, and amounts calculated with them may be off by a few cents.
Dates and times: For a moment in time, such as when an order was placed, use timestamptz in PostgreSQL. In MySQL, you choose between TIMESTAMP, which converts between time zones and is limited to 2038, and DATETIME, which stores the value as entered and goes up to the year 9999. Either way, using the same time zone on the server, the connection, and the application avoids confusion.
IDs: For a numeric primary key, use BIGINT with AUTO_INCREMENT, or in PostgreSQL, bigint GENERATED ALWAYS AS IDENTITY. When the ID is generated outside the database or appears in URLs, a UUID is a better fit, and a time-ordered UUID is better for indexes.
You'll find the commands for creating tables, backing up, and managing privileges on the MySQL commands and PostgreSQL commands pages, and to format a long query, you can use the SQL formatter tool.
With Linux hosting, you create MySQL and MariaDB databases as you need them, with no limit, and manage them with phpMyAdmin. A Linux VPS gives you full root access over SSH, so you can install any database that's compatible with the distribution you chose.
SQL data types FAQ
Which data type should I use for money amounts?
For money amounts, use DECIMAL in MySQL and MariaDB, or numeric in PostgreSQL, with a fixed number of digits after the decimal point, for example DECIMAL(12,2). These types store the value exactly, with no rounding errors. FLOAT and DOUBLE store an approximate value, so they aren't suited to accounts, prices, and balances.
What's the difference between DATETIME and TIMESTAMP in MySQL?
DATETIME stores the date and time exactly as entered, in the range from the year 1000 to the year 9999. TIMESTAMP converts the value from the time zone of the connection to UTC when it's stored, and back when it's retrieved, and its range in MySQL ends on January 19, 2038. For dates far in the future, use DATETIME. In PostgreSQL, the type equivalent to TIMESTAMP is timestamptz, which has a much larger range.
What's the PostgreSQL equivalent of AUTO_INCREMENT?
In PostgreSQL, you define an identity column, for example id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, which automatically gets an increasing number for each new row. The serial and bigserial types do something similar, but they're a shorthand that creates a separate sequence, and they don't add a uniqueness constraint. When you move from MySQL, an INT UNSIGNED column with AUTO_INCREMENT is usually converted to bigint.
Should I choose json or jsonb in PostgreSQL?
In most cases, choose jsonb. jsonb stores the document in a binary format, so queries on it are faster, and it supports GIN indexes. json stores the text exactly as entered, including whitespace, key order, and duplicate keys, and it suits only cases where keeping the original text matters.
What's the difference between utf8mb4 and utf8 in MySQL?
In MySQL, utf8 is an alias for the utf8mb3 character set, which stores up to 3 bytes per character and doesn't support emoji and some special characters. utf8mb4 stores up to 4 bytes per character and supports all Unicode characters, including emoji. utf8mb3 is deprecated, and the MySQL documentation recommends utf8mb4 for every new application.