I am working on a PostgreSQL database. I would like to get the results of a SELECT
query as a list of INSERT
statements and have them saved into a .sql
file (let's say exported_data.sql
).
This is because later I would like to run the .sql
file on another database and have the data "imported" in the table, so that the table gets "updated".
psql my_database -h my_host < exported_data.sql
Let's say my query is:
SELECT *
FROM params_table
WHERE params_type = 'tkt';
From this thread I see that one way to do it is to:
Create a temporary table filled with the results of the query
CREATE TABLE temporary_table AS SELECT * FROM params_table WHERE params_type = 'tkt';
Export the table as a list of
INSERT
statements into a.sql
filepg_dump --table=temporary_table --data-only --column-inserts my_database -h my_host > exported_data.sql
Remove the table, to keep the database clean.
DROP TABLE temporary_table;
Is there a way to do it in one shot?
I am asking just because I would not like to create and then remove a temporary table every time I need to export some data.
I saw from this thread that for a MySQL database it is possible to do it one-shot:
mysqldump -u root -p my_database -h my_host params_table --where="params_type = 'tkt'" > exported_data.sql
So I have read the Postgresql documentation for pg_dump
, but I cannot find any equivalent for --where
clause.