TheStranger
TheStranger

Reputation: 1597

How to create a column with the datatype float which only stores up to 3 decimals in PostgreSQL?

I want to create a table in my PostgreSQL:

CREATE TABLE my_table(
    id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
    description TEXT,
    score FLOAT NOT NULL
);

How do I limit the number of decimals stored in the "score" column to a maximum of 3 decimals?

Upvotes: 1

Views: 2703

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270833

You would use numeric. However, you need a precision as well, which limits the maximum value:

CREATE TABLE my_table(
    id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
    description TEXT,
    score NUMERIC(10, 3) NOT NULL
);

This will store numbers up to 9,999,999.999.

Upvotes: 1

Related Questions