PostgreSQL - Working with a virtual/generated column
PostgreSQL - Working with a virtual/generated column
To create a table in PostgreSQL with a virtual/generated column derived from JSON data, you can use the jsonb
data type along with a generated column. Here is an example:
Sample Json:
{ "firstname":"John", "lastName":"Sri" }
Create the table: Define the table with a
jsonb
column and a generated column that extracts a value from the JSON data.Insert data & Query data: Insert JSON data into the table and Query the table to see the generated column in action.
Here is a step-by-step guide:
Step 1: Create the Table
In this example:
id
: A serial primary key column.data
: A JSONB column to store the JSON data.first_name
: A generated column that extracts thefirst_name
from the JSON data.last_name
: A generated column that extracts thelast_name
from the JSON data.
Here’s how you might use this table:
In this way,
first_name
and last_name
columns are automatically populated based on the values extracted from the data
JSON column.
Comments