Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql what is the best way to export specific column from specific table from a DB to another

When I do this command :

SELECT language, title FROM cms_title WHERE language != 'en' AND title != 'Blog';

I see the pages I want to copy from a DB on the development server to the production server.

So, my aim is to dump those specific pages and then insert them in the production DB.

My question is : Is it possible ? Is it the best practice / way of doing it ?

Thank you so much in advance for the time spent on my request.

like image 226
Jay Cee Avatar asked Nov 20 '15 12:11

Jay Cee


People also ask

How do you copy data from one table to another table in different database in Postgres?

First, make sure you are connected with both DataSources in Data Grip. Select Source Table and press F5 or (Right-click -> Select Copy Table to.) This will show you a list of all tables (you can also search using a table name in the popup window). Just select your target and press OK.

What is the best way to transfer the data in a PostgreSQL database?

If you really have two distinct PostgreSQL databases, the common way of transferring data from one to another would be to export your tables (with pg_dump -t ) to a file, and import them into the other database (with psql ).

How do I SELECT a specific column in PostgreSQL?

PostgreSQL SELECT statement syntax If you specify a list of columns, you need to place a comma ( , ) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names.


1 Answers

I do it the following way:

1.create dump:

psql -h dbhost1 -d dbname -U dbuser -c "copy(SELECT language, title FROM cms_title WHERE language != 'en' AND title != 'Blog') to stdout" > dump.tsv`

2.import dump:

psql -h dbhost2 -d dbname -U dbuser -c 'copy cms_title from stdin' < dump.tsv

This will append imported data to the table.

like image 92
stas.yaranov Avatar answered Oct 26 '22 20:10

stas.yaranov