In this cheat sheet, we have compiled only the most essential information about each of the Postgres data types. We provide usage examples, acceptable ranges, expected disk space requirements, and quick tips so you can easily pick the right Postgres data types for your projects.
Used for storing strings of various lengths depending on your needs. Although there is no performance difference between them, you may save disk space using VARCHAR(n) or TEXT over CHAR(n) due to the extra disk space it uses for padding.
Variable length datatype useful for storing small strings.
ALTER TABLE users ADD COLUMN user_name CHAR(15);
Tips
- Will ALWAYS store n number of characters.
- Remaining spaces will not be padded.
- Strings larger than 'n' will throw an error.
- Spaces do not count toward character limit but will be truncated.
Variable length datatype useful for storing large strings
ALTER TABLE users ADD COLUMN password VARCHAR(128);
Tips
- Will ONLY store given number of characters.
- Remaining spaces will not be padded.
- Strings larger than 'n' will throw an error.
- Spaces do not count toward character limit but will be truncated.
- Acts the same as TEXT datatype when n is not given.
Useful for storing strings of any length.
ALTER TABLE posts ADD COLUMN blog_content TEXT;
Tips
- Will ONLY store given number of characters.
- Remaining spaces will not be padded.
Used in Postgres for storing floating-point numbers or integers (whole numbers). Each data type has its own benefits and drawbacks depending on your needs.
Useful for storing small whole numbers.
ALTER TABLE users ADD COLUMN age SMALLINT;
Tips
- Integers outside of range will throw an error.
- Can not store decimal places.
- Also known as INT2.
Useful for storing medium whole numbers.
ALTER TABLE posts ADD COLUMN likes INTEGER;
Tips
- Integers outside of range will throw an error.
- Can not store decimal places.
- Also known as INT and INT4
Useful for storing large whole numbers.
ALTER TABLE orders ADD COLUMN epoch BIGINT;
Tips
- Integers outside of range will throw an error.
- Can not store decimal places.
- Also known as INT
Useful for storing large precise, floating point numbers.
ALTER TABLE products ADD COLUMN price NUMERIC(4, 2);
Tips
- Precision is user-specified.
- Precision must be positive.
- Also known as DECIMAL.
- Good for storing financial data where high precision is required.
Useful for storing small, floating point numbers when precision is not a concern.
ALTER TABLE recipes ADD COLUMN ingredient_grams REAL;
Tips
- Not recommended for financial data.
- Prone to rounding errors.
- Also known as FLOAT and FLOAT4
Useful for storing large, floating point numbers when precision is not a concern.
ALTER TABLE recipes ADD COLUMN ingredient_grams DOUBLE PRECISION;
Tips
- Not recommended for financial values.
- Prone to rounding errors.
- Also known as FLOAT8
Useful for storing small autoincrementing whole numbers.
ALTER TABLE players ADD COLUMN score SMALLSERIAL;
Tips
- Not a true data type.
- Only a notational convenience for creating unique identifiers.
Useful for storing medium autoincrementing whole numbers.
ALTER TABLE accounts ADD COLUMN order_id SERIAL;
Tips
- Not a true data type.
- Only a notational convenience for creating unique identifiers.
Useful for storing large autoincrementing whole numbers.
ALTER TABLE orders ADD COLUMN order_id BIGSERIAL;
Tips
- Not a true data type.
- Only a notational convenience for creating unique identifiers.
The Boolean data type in Postgres is used for storing one of three values (true, false or null).
Useful for storing a true or false values.
ALTER TABLE blog ADD COLUMN published BOOLEAN;
Tips
- true can also be yes, on or 1.
- false can also be no, off or 0
- Accepts a third state of unknown (null).
- Also known as Bool.
The UUID (Universally Unique IDentifiers) data type allows you to store UUIDs (a proposed standard by the Internet Engineering Task Force) in Postgres.
Useful for generating extremely unique ids.
ALTER TABLE users ADD COLUMN user_id uuid UNIQUE DEFAULT uuid_generate_v4 ();
Tips
- More unique than sequence generators.
- More disk space required than sequence generated numbers.
- Unlikely to be generated by anyone else using the same algorithm.
- uuid-ossp extension is required.
These JSON data types allow you to store JSON in either plain text or in a binary format in Postgres.
Useful for storing JSON in plain text.
ALTER TABLE users ADD COLUMN address JSON;
Tips
- Stores JSON 'as is' in plain text.
- Faster than JSONB type to input.
- Slower than JSONB type to process.
- Does not support indexing.
Useful for storing JSON in binary format.
ALTER TABLE users ADD COLUMN address JSONB;
Tips
- Slower than JSON type to input.
- Faster than JSON type to process.
- Support indexing.
- Unless there is some special requirement, JSONB is a better choice than JSON.
Storing date and time is an essential feature of any RDBMS and Postgres has many ways to do it with even more flexability than the SQL standard requires. Once date or time data is stored, Postgres has many advanced features for working with time and date data types.
Useful for storing a date in the year/month/day format.
ALTER TABLE users ADD COLUMN birthday DATE;
Tips
- Stores and inserts the date in yyyy-mm-dd format
- 1 day resolution
The TIME data type can store time information with or without the timezone.
ALTER TABLE timezones ADD COLUMN london TIME with time zone;
Tips
- Columns can be created to store time with or without timezone info.
- May have a precision up to 6 digits
- 1 microsecond resolution
Stores a timestamp and is interpreted as local date-time.
ALTER TABLE orders ADD COLUMN purchase_date TIMESTAMP;
Tips
- Interpreted as local time.
- 1 microsecond resolution
Stores a timestamp and is interpreted as a point on the UTC time line.
ALTER TABLE orders ADD COLUMN purchase_date TIMESTAMPTZ;
Tips
- Interpreted as UTC time.
- 1 microsecond resolution
Useful for storing and working with periods of time.
16 bytes.
ALTER TABLE orders ADD COLUMN subscription_length INTERVAL;
Used for storing currency amounts with fixed fractional precision.
ALTER TABLE blog ADD COLUMN price MONEY;
Used for storing binary strings.
ALTER TABLE blog ADD COLUMN image BYTEA;
Tips
- Not suited for storing extremly large amounts of binary data
When working with PostgreSQL, finding the right data type for the data you're working with is essential. This cheat sheet is intended to be a handy reference for you to keep coming back to for examples, ranges, space requirements, and tips so that you can pick the right data type every time.
Crypto Alerts
© Cloud Giant 2024. All rights reserved