Home >Database >Mysql Tutorial >How Can I Easily Import JSON Files into PostgreSQL Using the Command Line?

How Can I Easily Import JSON Files into PostgreSQL Using the Command Line?

DDD
DDDOriginal
2025-01-13 08:38:43300browse

How Can I Easily Import JSON Files into PostgreSQL Using the Command Line?

Simplified PostgreSQL JSON file import method

Importing JSON files into a PostgreSQL database often requires a tedious approach involving typed columns and intermediate tables. However, PostgreSQL provides an easy way to use the command line psql tool.

The key to this method is to use backticks to load the JSON file into a psql variable:

<code>\set content `cat /path/to/json_file.json`</code>

After loading the JSON, create a temporary table to store it:

<code>create temp table t ( j jsonb );</code>

Next, insert the contents of the variable into the table:

<code>insert into t values (:'content');</code>

To retrieve the imported data, simply execute a select statement against the table:

<code>select * from t;</code>

This technology allows you to import multi-row JSON objects directly into JSON columns without explicit embedding or complex SQL queries. You can also use this method directly to perform operations on the data.

The above is the detailed content of How Can I Easily Import JSON Files into PostgreSQL Using the Command Line?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn