Convert JSON string to JSONB - postgresql

I have a table with jsonb field. Some of the rows are an array of objects, but some of the others are string.
I want to convert red rows to array of objects.
My table structure:
How I can do this in PostgreSQL?

Following SQL should do the trick:
update your_table_name
set content = (content#>>'{}')::jsonb
where jsonb_typeof(content)='string';
Reference:
https://www.postgresql.org/docs/10/functions-json.html

Related

PostgreSQL get array column value from second index

I have a table with a column that contains array of numbers like : ['1','3','45'];
And was looking for a way to select the column value from the second index so that I get ['3','45']
Is there any way to do this in PostgreSQL?

PostgreSQL: Index JSONB array that is queried with `#?` operator

My table (table) has a JSONB field (data) that contains a field with an array where I store tags (tags).
I query that table with an expression like:
SELECT * FROM table WHERE data->'tags' #? '$[*] ? (# like_regex ".*(foo|bar).*" flag "i");
With such use-case is there a way for me to index the data->'tags' array to speed up the query? Or should I rather work on moving the tags array out of the JSONB field and into a TEXT[] field and index that?
I've already tried:
CREATE INDEX foo ON tbl USING GIN ((data->'tags') jsonb_path_ops);
but it doesn't work: https://gist.github.com/vkaracic/a62ac917d34eb6e975c4daeefbd316e8
The index you built can be used (if you set enable_seqscan=off, you will see that it does get used), but it is generally not chosen as it is pretty useless for this query. The only rows it would rule out through the index are the ones that don't have the 'tags' key at all, and even at that is poorly estimated so probably won't be used without drastic measures.
You could try to convert to text[] and the use parray_gin, but probably better would be to convert to a child table with text and then use pg_trgm.

Typeorm Jsonb column array counts

In my database i have a jsonb array column and i want to count element inside that column during my select query from typeorm.
I am using postgres.

how to change the length of varchar of array in postgresql database without losing data

I tried with the code below, but raised a "syntax error at or near array". Googled around, and nothing found. Is it possible to do so? Thanks!
alter table "tablename" alter column "columnname" TYPE ARRAY(VARCHAR(200));
It's unclear to me if you want to increase the length of each entry, or the length of the array.
An array declaration follows the form datatype[] - the [] makes the column an array and the data type specification is the one for the base type.
So, if you want to increase the length of each array element, just declare an array with a longer varchar length: varchar(200)[]:
alter table "tablename"
alter column "columnname" TYPE varchar(200)[];
If you want to use the ARRAY keyword, that needs to be put after the data type:
alter table "tablename"
alter column "columnname" TYPE varchar(200) array;
If you want to increase the length of the array (=allow more array elements) you don't need to do anything because even if you did specify an array dimension, this is not enforced by Postgres

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
How to add a new value to text[] column?
What will be the resultset when we query to retrieve the values of text[] column?
EDIT
I have a table containing 2 columns group_name and members. Each time a new person join the group,the new person's id should be inserted in the column members for that group_name. This is my requirement.A Group can contain 'n' number of members
EDIT 2
Pablo is asking me to use two tables instead. May I know how this could be solved by using two different tables? Right now I am using comma(,) to store multiple values separated by comma. Is this method wrong?
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.