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 columnsid
anddata
wheredata
is of typejson
.INSERT INTO json_data (data) VALUES ('{"name": "John Doe", "age": 32}')
- Inserts a JSON object into thedata
column.SELECT * FROM json_data
- Retrieves all rows from thejson_data
table.
Helpful links
More of Postgresql
- How do I use the PostgreSQL hash function?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I set a PostgreSQL interval to zero?
- How can I convert XML data to a PostgreSQL table?
- How can I extract the year from a date in PostgreSQL?
- How do I use the PostgreSQL XML type?
- How do I set the PostgreSQL work_mem parameter?
See more codes...