suraj
suraj

Reputation: 1898

text[] in postgresql?

I saw a field text[] (text array) in Postgresql.

As far as I understood,it can store multiple text data in a single column.

I tried to read more about it the manual: http://www.postgresql.org/docs/current/static/datatype-character.html but unfortunately nothing much was there about text[] column type.

So can anyone help me to understand

Upvotes: 8

Views: 18074

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181380

To insert new values just do:

insert into foo values (ARRAY['a', 'b']);

Assuming you have this table:

create table foo (a text[]);

Every time you do a select a from foo you will have a column of type array:

db1=> select a from foo;
   a   
-------
 {a,b}
(1 row)

If you want a specific element from the array, you need to use subscripts (arrays in PostgreSQL are 1-based):

db=> select a[1] from foo;
 a 
---
 a
(1 row)

Be careful when choosing an array datatype for your PostgreSQL tables. Make sure you don't need a child table instead.

Upvotes: 5

Related Questions