postgresqlHow do I store and query JSON data in PostgreSQL?
PostgreSQL supports the storage of JSON data in its native JSON data type. This allows you to store and query JSON data directly in the database.
Example code
CREATE TABLE json_data (
id serial PRIMARY KEY,
data json
);
INSERT INTO json_data (data)
VALUES
('{"name": "John Doe", "age": 32}');
SELECT * FROM json_data;
Output example
id | data
----+----------------------------------
1 | {"name": "John Doe", "age": 32}
Code explanation
CREATE TABLE json_data (id serial PRIMARY KEY, data json)- Creates a table with the columnsidanddatawheredatais of typejson.INSERT INTO json_data (data) VALUES ('{"name": "John Doe", "age": 32}')- Inserts a JSON object into thedatacolumn.SELECT * FROM json_data- Retrieves all rows from thejson_datatable.
Helpful links
More of Postgresql
- How can I set a PostgreSQL interval to zero?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL and ZFS snapshots together?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I use PostgreSQL with Qt?
- How can I use PostgreSQL's "zero if null" feature?
- How can Zalando use PostgreSQL to improve its software development?
- How can I monitor PostgreSQL performance using Zabbix?
- How do I use PostgreSQL query parameters?
- How can I use PostgreSQL hints to optimize my query performance?
See more codes...