Copy a table from one database to another in Postgres

前端 未结 19 1177
慢半拍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 01:05

    If the both DBs(from & to) are password protected, in that scenario terminal won't ask for the password for both the DBs, password prompt will appear only once. So, to fix this, pass the password along with the commands.

    PGPASSWORD=<password> pg_dump -h <hostIpAddress> -U <hostDbUserName> -t <hostTable> > <hostDatabase> | PGPASSWORD=<pwd> psql -h <toHostIpAddress> -d <toDatabase> -U <toDbUser>
    
    0 讨论(0)
  • 2020-11-28 01:06

    As an alternative, you could also expose your remote tables as local tables using the foreign data wrapper extension. You can then insert into your tables by selecting from the tables in the remote database. The only downside is that it isn't very fast.

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

    I was using DataGrip (By Intellij Idea). and it was very easy copying data from one table (in a different database to another).

    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.

    DataGrip will handle everything else for you.

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

    Use pg_dump to dump table data, and then restore it with psql.

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

    If you run pgAdmin (Backup: pg_dump, Restore: pg_restore) from Windows it will try to output the file by default to c:\Windows\System32 and that's why you will get Permission/Access denied error and not because the user postgres is not elevated enough. Run pgAdmin as Administrator or just choose a location for the output other than system folders of Windows.

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

    I tried some of the solutions here and they were really helpful. In my experience best solution is to use psql command line, but sometimes i don't feel like using psql command line. So here is another solution for pgAdminIII

    create table table1 as(
     select t1.* 
     from dblink(
       'dbname=dbSource user=user1 password=passwordUser1',
       'select * from table1'  
      ) as t1(
        fieldName1 as bigserial,
        fieldName2 as text,
        fieldName3 as double precision 
      )
     )
    

    The problem with this method is that the name of the fields and their types of the table you want to copy must be written.

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