Today I Learned

hashrocket A Hashrocket project

Defining Arrays In PostgreSQL

In postgres, an array can be defined using the array syntax like so:

> select array['a','b','c'];
  array
---------
 {a,b,c}

If you are inserting into an existing array column, you can use the array literal syntax.

> create temp table favorite_numbers(numbers integer[]);
CREATE TABLE
> insert into favorite_numbers values( '{7,3,9}' );
INSERT 0 1
> select numbers[2] from favorite_numbers;
 numbers
---------
       3

Postgres also supports two-dimensional arrays.

select array[[1,2,3],[4,5,6],[7,8,9]] telephone;
         telephone
---------------------------
 {{1,2,3},{4,5,6},{7,8,9}}
See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.