PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Tutorial / PostgreSQL Array

PostgreSQL Array

Summary: in this tutorial, we will show you how to work with PostgreSQL array and introduce you to some handy functions for array manipulation.

Array plays an important role in PostgreSQL. Every data type has its own companion array type e.g., integer has an integer[] array type, character has character[] array type, etc. In case you define your own data type, PostgreSQL creates a corresponding array type in the background for you.

PostgreSQL allows you to define a column to be an array of any valid data type including built-in type, user-defined type or enumerated type.

The following CREATE TABLE statement creates the contacts table with the phones column is defined as an array of text.

1
2
3
4
5
CREATE TABLE contacts (
id serial PRIMARY KEY,
name VARCHAR (100),
phones TEXT []
);

The phones column is a one-dimensional array that holds various phone numbers that a contact may have.

Insert PostgreSQL array values

The following statement inserts a new contact into the contacts table.

1
2
3
4
5
6
7
INSERT INTO contacts (name, phones)
VALUES
(
'John Doe',
ARRAY [ '(408)-589-5846',
'(408)-589-5555' ]
);

We used the ARRAY constructor to construct an array and insert it into the contacts table. You can also use curly braces as follows:

1
2
3
4
5
6
7
8
9
10
INSERT INTO contacts (name, phones)
VALUES
(
'Lily Bush',
'{"(408)-589-5841"}'
),
(
'William Gate',
'{"(408)-589-5842","(408)-589-58423"}'
);

The statement above inserts two rows into the contacts table. Notice that when you use curly braces, you use single quotes ' to wrap the array and double quotes " to wrap text array items.

Query array data

You use the SELECT statement to query array data as follows:

1
2
3
4
5
SELECT
name,
phones
FROM
contacts;

postgresql array query data

We access array elements using the subscript within square brackets []. By default, PostgreSQL uses one-based numbering for array elements. It means the first array element starts with number 1. Suppose, we want to get the contact’s name and the first phone number, we use the following query:

1
2
3
4
5
SELECT
name,
phones [ 1 ]
FROM
contacts;

postgresql array query array item

We can use array element in the WHERE clause as the condition to filter the rows. For example, to find out who has the phone number (408)-589-58423 as the second phone number, we use the following query.

1
2
3
4
5
6
SELECT
name
FROM
contacts
WHERE
phones [ 2 ] = '(408)-589-58423';

postgresql array element in where clause

Modifying PostgreSQL array

PostgreSQL allows you to update each element of an array or  the whole array. The following statement updates the second phone number of William Gate.

1
2
3
4
UPDATE contacts
SET phones [ 2 ] = '(408)-589-5843'
WHERE
ID = 3;

Let’s check it again.

1
2
3
4
5
6
7
8
SELECT
id,
name,
phones [ 2 ]
FROM
contacts
WHERE
id = 3;

postgresql array update

The following statement updates an array as a whole.

1
2
3
4
UPDATE contacts
SET phones = '{"(408)-589-5843"}'
WHERE
ID = 3;

We verify the update using the following statement.

1
2
3
4
5
6
7
SELECT
name,
phones
FROM
contacts
WHERE
id = 3;

postgresql array select

Search in PostgreSQL Array

Suppose, we want to know who has the phone number (408)-589-5555 regardless of position of the phone number in the phones array, we use ANY() function as follows:

1
2
3
4
5
6
7
SELECT
name,
phones
FROM
contacts
WHERE
'(408)-589-5555' = ANY (phones);

postgresql array any function

Expand Arrays

PostgreSQL provides the unnest() function to expand an array to a list of rows. For example,  the following query expands all phone numbers of the phones array.

1
2
3
4
5
SELECT
name,
unnest(phones)
FROM
contacts;

postgresql array unnest function

In this tutorial, we have shown you how to work with PostgreSQL array data type and introduced you to some of the most important array operators and functions.

Previous Tutorial: PostgreSQL DROP DATABASE
Next Tutorial: PostgreSQL hstore

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.