Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store lists in Postgresql?

Tags:

postgresql

What is the best way to store lists in postgresql? What kind of field do I have to use? do I have to serialize it?

This is an example of data that I want to store:

id ( number ), name ( text ), events ( list )

1, john connor, ["aaa","bbb","ccc"]

2, jack bush, ["ttt","hhh","lll"]

...
like image 390
xRobot Avatar asked Apr 22 '26 06:04

xRobot


1 Answers

How about using arrays? They are declared with [] for any size and [3] for array with 3 cells.

CREATE TABLE test1 (
  id integer,
  name text,
  events text[]);
  
INSERT into test1 (id, name, events)
    values(1, 'john connor', '{"aaa", "bbb","ccc"}'),
          (2, 'jack bush', '{"ttt", "hhh","lll"}');

When inputting arrays, everything has to be enclosed in curly braces AND the curly braces have to be enclosed in single quotes. Finally, strings inside arrays must use double quotes.

Here's what the table looks like:

id  name            events
1   john connor     {aaa,bbb,ccc}
2   jack bush       {ttt,hhh,lll}
like image 140
bfris Avatar answered Apr 25 '26 00:04

bfris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!