11

Storing Semi-Structured Data in PostgreSQL

 3 years ago
source link: https://knasmueller.net/storing-semi-structured-data-in-postgresql
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Databases

Storing Semi-Structured Data in PostgreSQL

JSON-1600x1067.jpg

Traditionally, relational databases required all tables to have a fixed data schema, i.e. a set of attributes (such as a “user” table with attributes “firstName”, “lastName” and “email”). You could change the schema by adding and removing attributes, but the available attributes were always know at a given point in time.

For many use cases, this was acceptable. However, some applications have the need to store semi-structured data: entries where the attributes are not known ahead of time.

Since 2012, PostgreSQL allows to store semi-structured data inside tables in the JSON notation:

{
  "ID": "123",
  "Country": "Denmark",
  "Title": "How to store JSON in PostgreSQL"
}

This allows having different sets of attributes for different rows in your table while leaving your table schema constant.

Pasted-into-Storing-Semi-Structured-Data-in-PostgreSQL.png

Extracting JSON data into their own columns

But what if you later decide that some of the attributes stored in your JSON column should rather have their own column so you can more easily define foreign keys, triggers and all the other useful things a relational database allows you to?

There is a simple way to extract values from JSON data into their own column.

First, create the new column with the appropriate data type (e.g., VARCHAR):

ALTER TABLE user ADD COLUMN country VARCHAR;

Then run an update statement over your whole table to populate the new column:

UPDATE user SET country = (rawData->>'Country')::VARCHAR WHERE ID >= 0;

This assumes your JSON data was stored in the column “rawData”. It is converted to a VARCHAR with the double-colon operator (::) and stored in the new column “country”.

Pasted-into-Storing-Semi-Structured-Data-in-PostgreSQL-1.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK