简体   繁体   中英

Correct psql COPY command for transferring table data?

I am writing some c code to transfer data from database_1, table_1 to database_2, table_2. I am quite new to postgresql and unable to figure out how to do this. I am using postgres-9.5

Is the below syntax correct?

psql database_1 -c 'COPY table_1 TO stdout' | psql database_2 -c 'COPY table_2 FROM stdin'

The intelligent guys here, please help me with the correct syntax. Thank you in advance!!

not sure why you don't try yourself, but yes - this should work:

postgres=# create database database_1;
CREATE DATABASE
postgres=# create database database_2;
CREATE DATABASE
postgres=# \c database_1
You are now connected to database "database_1" as user "vao".
database_1=# create table table_1 (i int);
CREATE TABLE
database_1=# \q
Vaos-MacBook-Air:~ vao$ psql database_2
psql (9.5.3)
Type "help" for help.
database_2=# create table table_2 (i int);
CREATE TABLE
database_2=# \q
Vaos-MacBook-Air:~ vao$ psql database_1 -c 'COPY table_1 TO stdout' | psql database_2 -c 'COPY table_2 FROM stdin'
COPY 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM