Skip to content

Instantly share code, notes, and snippets.

@jakubriedl
Last active August 29, 2015 14:17
Show Gist options
  • Save jakubriedl/cfdd7ab2975f2a9ab699 to your computer and use it in GitHub Desktop.
Save jakubriedl/cfdd7ab2975f2a9ab699 to your computer and use it in GitHub Desktop.
Composite Types in Postgres

Price

Create custom type

CREATE TYPE price AS ("amount" float, "currency" char(3));
CREATE TYPE vat AS ("base" float, "amount" float, "rate" smallint);

Use the type in table column

CREATE TABLE sandbox.test (
    id serial,
    "price" price,
    "vat" vat
);

Insert into the column

INSERT INTO sandbox.test (price,vat) SELECT ROW(1.254, 'EUR')::price, ROW(100,21,21)::vat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment