How to append the postgres data in production server ?
I have a table name is nijam, that table contains 63017 records and have csv excel data contains 586128 records so want to append the data in nijam table for this task you have to following below steps
Sample DATA ‘s are
id,code,name,date,
63202,63202,tdba,,now(),
Note : after tdba null value and system time is there, while copying it need to insert null values then server time for those purpose you can use "NULL" and now() keyword
Taking nijam table backup before appending the records below command will help you to take table level backup
create table postgre.nijam_temp As select * from postgre.nijam;
nijam_db=# create table postgre.nijam_temp As select * from postgre.nijam;
SELECT 63017
Then count the records before appending the data below command will help you to count the records :
select count(*) from postgre.nijam_temp;
nijam_db=# select count(*) from postgre.nijam_temp;
count
-------
63017
(1 row)
Then issue the COPY command against nijam_temp table then count the records if everything is ok ,then you can do directly into source table means nijam table, Below command will help you to copy or append data into table
\COPY postgre.nijam_temp FROM '/home/missing_data.csv' delimiter ',' CSV header NULL AS 'null';
nijam_db=# \COPY postgre.nijam_temp FROM '/home/missing_data.csv' delimiter ',' CSV header NULL AS 'null';
COPY 586128
Explanation :
- , – Data splitting by delimiter
- NULL AS ‘null’ – Inserting NULL values id record is empty
- HEADER – Excel sheet having header name like id,name,date
Counting temporary table’s records
nijam_db=# select count(*) from postgre.nijam_temp;
count
--------
649145
(1 row)
Why we are appending data initially on temporary table means if we get any issue or error while copying data means source table may be collapse for those purpose we are working on temporary table.
APPENDING DATA INTO SOURCE TABLE OF NIJAM :
Below steps for appending the data into original table which you have done previously do same like
Counting the source table records
select count(*) from postgre.nijam;
Appending the data into source table
\COPY postgre.nijam FROM '/home/missing_data.csv' delimiter ',' CSV header NULL AS 'null';
checking the table rows count below command will help to count the table records
select count(*) from postgre.nijam