Copy a table from one database to another in Postgres

前端 未结 19 1175
慢半拍i
慢半拍i 2020-11-28 00:21

I am trying to copy an entire table from one database to another in Postgres. Any suggestions?

相关标签:
19条回答
  • 2020-11-28 00:55

    You have to use DbLink to copy one table data into another table at different database. You have to install and configure DbLink extension to execute cross database query.

    I have already created detailed post on this topic. Please visit this link

    0 讨论(0)
  • 2020-11-28 00:56

    You could do the following:

    pg_dump -h <host ip address> -U <host db user name> -t <host table> > <host database> | psql -h localhost -d <local database> -U <local db user>
    

    0 讨论(0)
  • 2020-11-28 01:00

    Using psql, on linux host that have connectivity to both servers

    ( export PGPASSWORD=password1 
      psql -U user1 -h host1 database1 \
      -c "copy (select field1,field2 from table1) to stdout with csv" ) \
    | 
    ( export PGPASSWORD=password2 
      psql -U user2 -h host2 database2 \ 
       -c "copy table2 (field1, field2) from stdin csv" )
    
    0 讨论(0)
  • 2020-11-28 01:02

    Same as answers by user5542464 and Piyush S. Wanare but split in two steps:

    pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase > dump
    cat dump | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase
    

    otherwise the pipe asks the two passwords in the same time.

    0 讨论(0)
  • 2020-11-28 01:04

    Using dblink would be more convenient!

    truncate table tableA;
    
    insert into tableA
    select *
    from dblink('hostaddr=xxx.xxx.xxx.xxx dbname=mydb user=postgres',
                'select a,b from tableA')
           as t1(a text,b text);
    
    0 讨论(0)
  • 2020-11-28 01:05

    If you have both remote server then you can follow this:

    pg_dump -U Username -h DatabaseEndPoint -a -t TableToCopy SourceDatabase | psql -h DatabaseEndPoint -p portNumber -U Username -W TargetDatabase
    

    It will copy the mentioned table of source Database into same named table of target database, if you already have existing schema.

    0 讨论(0)
提交回复
热议问题