I am trying to copy an entire table from one database to another in Postgres. Any suggestions?
20 Answers
Extract the table and pipe it directly to the target database:
pg_dump -t table_to_copy source_db | psql target_db
Note: If the other database already has the table set up, you should use the -a
flag to import data only, else you may see weird errors like "Out of memory":
pg_dump -a -t my_table my_db | psql target_db
You can also use the backup functionality in pgAdmin II. Just follow these steps:
- In pgAdmin, right click the table you want to move, select "Backup"
- Pick the directory for the output file and set Format to "plain"
- Click the "Dump Options #1" tab, check "Only data" or "only Schema" (depending on what you are doing)
- Under the Queries section, click "Use Column Inserts" and "User Insert Commands".
- Click the "Backup" button. This outputs to a .backup file
- Open this new file using notepad. You will see the insert scripts needed for the table/data. Copy and paste these into the new database sql page in pgAdmin. Run as pgScript - Query->Execute as pgScript F6
Works well and can do multiple tables at a time.
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);
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" )
First install dblink
Then, you would do something like:
INSERT INTO t2 select * from
dblink('host=1.2.3.4
user=*****
password=******
dbname=D1', 'select * t1') tt(
id int,
col_1 character varying,
col_2 character varying,
col_3 int,
col_4 varchar
);
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.
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.
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.
pg_dump
does not work always.
Given that you have the same table ddl in the both dbs you could hack it from stdout and stdin as follows:
# grab the list of cols straight from bash
psql -d "$src_db" -t -c \
"SELECT column_name
FROM information_schema.columns
WHERE 1=1
AND table_name='"$table_to_copy"'"
# ^^^ filter autogenerated cols if needed
psql -d "$src_db" -c \
"copy ( SELECT col_1 , col2 FROM table_to_copy) TO STDOUT" |\
psql -d "$tgt_db" -c "\copy table_to_copy (col_1 , col2) FROM STDIN"
Check this python script
python db_copy_table.py "host=192.168.1.1 port=5432 user=admin password=admin dbname=mydb" "host=localhost port=5432 user=admin password=admin dbname=mydb" alarmrules -w "WHERE id=19" -v
Source number of rows = 2
INSERT INTO alarmrules (id,login,notifybyemail,notifybysms) VALUES (19,'mister1',true,false);
INSERT INTO alarmrules (id,login,notifybyemail,notifybysms) VALUES (19,'mister2',true,false);
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
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>
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.
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.
for DBeaver tool users, you can "Export data" to table in another database.
Only error I kept facing was because of wrong postgres driver.
SQL Error [34000]: ERROR: portal "c_2" does not exist
ERROR: Invalid protocol sequence 'P' while in PortalSuspended state.
Here is a official wiki on how to export data: https://github.com/dbeaver/dbeaver/wiki/Data-transfer