PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / An Overview Of PostgreSQL NUMERIC Type

An Overview Of PostgreSQL NUMERIC Type

Summary: this tutorial introduces you to the PostgreSQL NUMERIC type for storing numbers with very large number of digits.

Introduction to PostgreSQL NUMERIC data type

The NUMERIC type can store numbers with many digits. Typically, you use the NUMERIC type for monetary or other amounts which precision is required.

To declare a column of type NUMERIC, you use the following syntax:

1
NUMERIC(precision, scale)

The precision is the total number of digits, while scale is the number of digits in the fraction part. For example, the number 1234.567 has a precision of seven and a scale of three.

The NUMERIC value can have up to 131,072 digits before the decimal point 16,383 digits after the decimal point.

The scale of the NUMERIC type can be zero or positive, so the following syntax defines a NUMERIC column with scale of zero:

1
NUMERIC(precision)

If you omit both precision and scale, you can store any precision and scale up to the limit of the precision and scale mentioned above.

1
NUMERIC

In PostgreSQL, the NUMERIC and DECIMAL types are equivalent and both of them are also a part of SQL standard.

If precision is not required, you should not use the NUMERIC type because calculation on NUMERIC values are slower than integers, floats and double precision.

PostgreSQL NUMERIC examples

Storing numeric values

If you store a value with the scale greater than the declared scale of the NUMERIC column, PostgreSQL will round the value to the specified number of fractional digits. See the following example.

First, create a new table named products for the demonstration:

1
2
3
4
5
CREATE TABLE IF NOT EXISTS products (
    id serial PRIMARY KEY,
    name VARCHAR NOT NULL,
    price NUMERIC (5, 2)
);

Second, insert some products with the prices whose scales exceed the scale declared in the price column:

1
2
3
4
INSERT INTO products (NAME, price)
VALUES
    ('Phone',500.215),
    ('Tablet',500.214);

Because the scale of the price column is two, PostgreSQL rounds the value 500.215 up to 500.216 and rounds the value 500.214 down to 500.21 :

The following query returns all rows of the products table:

1
2
3
4
SELECT
    *
FROM
    products;

PostgreSQL NUMERIC example

In case you store a value whose precision exceeds the declared precision, PostgreSQL will raise an error as show in the following example:

1
2
3
INSERT INTO products (name, price)
VALUES
    ('Phone',123456.21);

In this example, PostgreSQL issued the following error:

1
2
ERROR:  numeric field overflow
DETAIL:  A field with precision 5, scale 2 must round to an absolute value less than 10^3.

PostgreSQL NUMERIC and NaN

Besides storing numeric values, the NUMERIC column also can store a special value called not-a-number or NaN.

1
2
3
4
UPDATE products
SET price = 'NaN'
WHERE
    id = 1;

In this example, we updated the price of the product whose id is 1 to NaN. Note that you must use quotes around 'NaN' as shown in the UPDATE statement above.

PostgreSQL NUMERIC NaN example

Typically, the NaN is not equal to any number including itself. It means that the expression NaN = NaN returns false. However, PostgreSQL treats NaN values are equal and NaN is greater than any non-NaN value. This implementation allows PostgreSQL to sort NUMERIC values and use them in the tree-based indexes.

The following query sorts the products based on prices:

1
2
3
4
5
6
SELECT
    *
FROM
    PRODUCTS
ORDER BY
    price DESC;

PostgreSQL NUMERIC NaN Sorting Example

As you can see, the NaN is greater than 500.21

In this tutorial, you have learned about PostgreSQL NUMERIC data types and how to use NUMERIC column for storing values that precision is required.

Previous Tutorial: PostgreSQL UNIQUE Constraint
Next Tutorial: A Look At Various PostgreSQL Integer Data Types

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • An Overview Of PostgreSQL Aggregate Functions
  • PostgreSQL POSITION
  • PostgreSQL MD5
  • PostgreSQL REGEXP_MATCHES
  • PostgreSQL REGEXP_REPLACE
  • PostgreSQL FORMAT
  • PostgreSQL TRANSLATE
  • PostgreSQL LPAD
  • PostgreSQL RIGHT
  • PostgreSQL LEFT

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.