Using psql is there a way to do a select statement where the output is a list of insert statements so that I can execute those insert statements somewhere else.
SELECT * FROM foo where some_fk=123; 
Should output
INSERT INTO foo 
(column1,column2,...) VALUES 
('abc','xyyz',...),
('aaa','cccc',...),
 .... ; 
That I can redicet to a file say export.sql which I can then import with psql -f export.sql
My goal is to move export the result of a select statement in a format that I can import into another database instance with exactly the same table structure. 
Have a look at the --inserts option of pg_dump
pg_dump -t your_table --inserts -f somefile.txt your_db
Edit the resulting file if necessary.
For a subset, as IgorRomanchenko mentioned, you can use COPY with a SELECT statement.
Example of COPYing as CSV.
COPY (select * from table where foo='bar') TO '/path/to/file.csv' CSV HEADER
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With