DQL

Tipos de Datos PostgreSQL

Tipos Numéricos

TipoEspacio de AlmacenamientoDescripciónRango
smallint2 bytessmall-range integer-32,768 to +32,767
integer4 bytestypical choice for integer-2,147’483,648 to +2,147’483,647
bigint8 byteslarge-range integer-9’223,372’036,854’775,808 to +9’223,372’036,854’775,807
decimalvariableuser-specified precision, exactup to 131,072 digits before the decimal point; up to 16,383 digits after the decimal point
numericvariableuser-specified precision, exactup to 131,072 digits before the decimal point; up to 16,383 digits after the decimal point
real4 bytesvariable-precision, inexact6 decimal digits precision
double precision8 bytesvariable-precision, inexact15 decimal digits precision
smallserial2 bytessmall autoincrementing integer1 to 32,767
serial4 bytesautoincrementing integer1 to 2,147’483,647
bigserial8 byteslarge autoincrementing integer1 to 9’223,372’036,854’775,807
NUMERIC(precision, scale)
NUMERIC(3, 1) -- -99.9 and 99.9, inclusive
'Infinity' or 'inf'
'-Infinity' or '-inf'
'NaN'

Tipos Moneda

TipoEspacio de AlmacenamientoDescripciónRango
money8 bytescurrency amount-92,233’720,368’547,758.08 to +92,233’720,368’547,758.07

Tipos Caracter

TipoDescripción
character varying(n), varchar(n)variable-length with limit
character(n), char(n), bpchar(n)fixed-length, blank-padded
bpcharvariable unlimited length, blank-trimmed
textvariable unlimited length

Tipos Binarios

TipoEspacio de AlmacenamientoDescripción
bytea1 or 4 bytes plus the actual binary stringvariable-length binary string

Una cadena binaria es una secuencia de octetos (o bytes). Las cadenas binarias permiten almacenar octetos de valor cero y otros octetos “no imprimibles” (generalmente, octetos fuera del rango decimal 32 a 126). Las cadenas de caracteres no permiten octetos cero ni otros valores de octeto y secuencias de valores de octeto que no sean válidos de acuerdo con la codificación de conjunto de caracteres seleccionada de la base de datos. Las operaciones en cadenas binarias procesan los bytes reales, mientras que el procesamiento de cadenas de caracteres depende de la configuración local. En resumen, las cadenas binarias son apropiadas para almacenar datos que el programador considera “bytes crudos”, mientras que las cadenas de caracteres son apropiadas para almacenar texto.

SET bytea_output = 'hex';

SELECT '\xDEADBEEF'::bytea;
   bytea
------------
 \xdeadbeef

SET bytea_output = 'escape';

SELECT 'abc \153\154\155 \052\251\124'::bytea;
     bytea
----------------
 abc klm *\251T
Valor Decimal del OctetoDescripciónRepresentación de Entrada EscapadaEjemploRepresentación Hex
0zero octet'\000''\000'::bytea\x00
39single quote'''' or '\047'''''::bytea\x27
92backslash'\\' or '\134''\\'::bytea\x5c
0 to 31 and 127 to 255“non-printable” octets'\xxx' (octal value)'\001'::bytea\x01

Tipos Fecha / Hora

TipoEspacio de AlmacenamientoDescripciónValor MinimoValor MáximoResolución
timestamp [ (p) ] [ without time zone ]8 bytesboth date and time (no time zone)4713 BC294276 AD1 microsecond
timestamp [ (p) ] with time zone8 bytesboth date and time, with time zone4713 BC294276 AD1 microsecond
date4 bytesdate (no time of day)4713 BC5874897 AD1 day
time [ (p) ] [ without time zone ]8 bytestime of day (no date)00:00:0024:00:001 microsecond
time [ (p) ] with time zone12 bytestime of day (no date), with time zone00:00:00+155924:00:00-15591 microsecond
interval [ fields ] [ (p) ]16 bytestime interval-178000000 years178000000 years1 microsecond

Tipos Booleanos

TipoTamaño de AlmacenamientoDescripción
boolean1 bytestate of true or false
-- “true” state:
true
yes
on
1

-- “false” state:
false
no
off
0

Tipos Enumerados

-- 1. Crear la enumeración
-- 2. Utilizar la enumeración

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');

CREATE TABLE person (
    name text,
    current_mood mood
);

INSERT INTO person VALUES ('Moe', 'happy');

SELECT * FROM person WHERE current_mood = 'happy';
-- name | current_mood
-- ------+--------------
-- Moe  | happy

Tipos Geométricos

TipoEsopacio de AlmacenamientoDescripciónRepresentación
point16 bytesPoint on a plane(x,y)
line24 bytesInfinite line{A,B,C}
lseg32 bytesFinite line segment[(x1,y1),(x2,y2)]
box32 bytesRectangular box(x1,y1),(x2,y2)
path16+16n bytesClosed path (similar to polygon)((x1,y1),…)
path16+16n bytesOpen path[(x1,y1),…]
polygon40+16n bytesPolygon (similar to closed path)((x1,y1),…)
circle24 bytesCircle<(x,y),r> (center point and radius)
-- Puntos
( x , y )
  x , y

-- Lineas:  Ax + By + C = 0
{ A, B, C }
[ ( x1 , y1 ) , ( x2 , y2 ) ]
( ( x1 , y1 ) , ( x2 , y2 ) )
  ( x1 , y1 ) , ( x2 , y2 )
    x1 , y1   ,   x2 , y2

-- Segmentos de Línea
[ ( x1 , y1 ) , ( x2 , y2 ) ]
( ( x1 , y1 ) , ( x2 , y2 ) )
  ( x1 , y1 ) , ( x2 , y2 )
    x1 , y1   ,   x2 , y2

-- Cajas / Rectangulos
( ( x1 , y1 ) , ( x2 , y2 ) )
  ( x1 , y1 ) , ( x2 , y2 )
    x1 , y1   ,   x2 , y2

-- Rutas
[ ( x1 , y1 ) , ... , ( xn , yn ) ]
( ( x1 , y1 ) , ... , ( xn , yn ) )
  ( x1 , y1 ) , ... , ( xn , yn )
  ( x1 , y1   , ... ,   xn , yn )
    x1 , y1   , ... ,   xn , yn

-- Poligonos
( ( x1 , y1 ) , ... , ( xn , yn ) )
  ( x1 , y1 ) , ... , ( xn , yn )
  ( x1 , y1   , ... ,   xn , yn )
    x1 , y1   , ... ,   xn , yn

-- Circulos
&lt; ( x , y ) , r >
( ( x , y ) , r )
  ( x , y ) , r
    x , y   , r

Tipos Dirección de Red (Network Address)

TipoEspacio de AlmacenamientoDescripción
cidr7 or 19 bytesIPv4 and IPv6 networks
inet7 or 19 bytesIPv4 and IPv6 hosts and networks
macaddr6 bytesMAC addresses
macaddr88 bytesMAC addresses (EUI-64 format)

Tipos Cadenas de Bits

Tipos: bit(n) y bit varying(n)

CREATE TABLE test (a BIT(3), b BIT VARYING(5));

INSERT INTO test VALUES (B'101', B'00');

INSERT INTO test VALUES (B'10', B'101');
ERROR:  bit string length 2 does not match type bit(3)

INSERT INTO test VALUES (B'10'::bit(3), B'101');

SELECT * FROM test;

  a  |  b
-----+-----
 101 | 00
 100 | 101

Tipos para Búsqueda de Texto

https://www.postgresql.org/docs/current/datatype-textsearch.html

Tipos UUID

https://www.postgresql.org/docs/current/datatype-uuid.html

Tipos XML

https://www.postgresql.org/docs/current/datatype-xml.html

Tipo JSON

https://www.postgresql.org/docs/current/datatype-json.html

Tipo Arreglo

https://www.postgresql.org/docs/current/arrays.html

Tipos Compuestos

https://www.postgresql.org/docs/current/rowtypes.html

Tipos Rango

https://www.postgresql.org/docs/current/rangetypes.html

Dominios de Tipos de Dato

https://www.postgresql.org/docs/current/domains.html

Tipos Identificador de Objeto

TipoReferencia a:DescripciónEjemplo
oidanynumeric object identifier564182
regclasspg_classrelation namepg_type
regcollationpg_collationcollation name"POSIX"
regconfigpg_ts_configtext search configurationenglish
regdictionarypg_ts_dicttext search dictionarysimple
regnamespacepg_namespacenamespace namepg_catalog
regoperpg_operatoroperator name+
regoperatorpg_operatoroperator with argument types*(integer,​integer) or -(NONE,​integer)
regprocpg_procfunction namesum
regprocedurepg_procfunction with argument typessum(int4)
regrolepg_authidrole namesmithee
regtypepg_typedata type nameinteger

https://www.postgresql.org/docs/current/datatype-oid.html

Pseudotipos

TipoDescripción
anyIndicates that a function accepts any input data type.
anyelementIndicates that a function accepts any data type (see Section 36.2.5).
anyarrayIndicates that a function accepts any array data type (see Section 36.2.5).
anynonarrayIndicates that a function accepts any non-array data type (see Section 36.2.5).
anyenumIndicates that a function accepts any enum data type (see Section 36.2.5 and Section 8.7).
anyrangeIndicates that a function accepts any range data type (see Section 36.2.5 and Section 8.17).
anymultirangeIndicates that a function accepts any multirange data type (see Section 36.2.5 and Section 8.17).
anycompatibleIndicates that a function accepts any data type, with automatic promotion of multiple arguments to a common data type (see Section 36.2.5).
anycompatiblearrayIndicates that a function accepts any array data type, with automatic promotion of multiple arguments to a common data type (see Section 36.2.5).
anycompatiblenonarrayIndicates that a function accepts any non-array data type, with automatic promotion of multiple arguments to a common data type (see Section 36.2.5).
anycompatiblerangeIndicates that a function accepts any range data type, with automatic promotion of multiple arguments to a common data type (see Section 36.2.5 and Section 8.17).
anycompatiblemultirangeIndicates that a function accepts any multirange data type, with automatic promotion of multiple arguments to a common data type (see Section 36.2.5 and Section 8.17).
cstringIndicates that a function accepts or returns a null-terminated C string.
internalIndicates that a function accepts or returns a server-internal data type.
language_handlerA procedural language call handler is declared to return language_handler.
fdw_handlerA foreign-data wrapper handler is declared to return fdw_handler.
table_am_handlerA table access method handler is declared to return table_am_handler.
index_am_handlerAn index access method handler is declared to return index_am_handler.
tsm_handlerA tablesample method handler is declared to return tsm_handler.
recordIdentifies a function taking or returning an unspecified row type.
triggerA trigger function is declared to return trigger.
event_triggerAn event trigger function is declared to return event_trigger.
pg_ddl_commandIdentifies a representation of DDL commands that is available to event triggers.
voidIndicates that a function returns no value.
unknownIdentifies a not-yet-resolved type, e.g., of an undecorated string literal.