0
I have a table with three columns, which already has data, in my Postgresql.
I have a file . CSV with four columns.
I would like to add the data of three columns of . CSV to SQL without replacing those that are already there.
How to do ?
SQL
Schema: cnpj
Table: cnae
Fields: cnpj, cnae, data
cnpj_dados_cnae_secundario_filt.CSV
Colunas: cnpj, cnae, nat_jur, data
- I haven’t tried anything yet because I’m afraid to overwrite the data that’s already there
Thank you.
Explain in detail the meaning of "without replacing those already there". What is the primary key of your table? There are other indexes?
– anonimo
There is no primary key, only these fields. " without replacing the ones that are already there" means that the table already has data and I want to add others, keeping the old ones.
– RxT
Use the command
COPY
(https://www.postgresql.org/docs/current/sql-copy.html) specifying the parameterFORMAT CSV
. It will add the new records, from the file, to the table. It seems that your table contains a non-existent field in your csv file, in this case this field will be with NULL.– anonimo
It would be something like: COPY INTO cnpj_dados_cadastrais_pj.cnae FROM 'G: cnpj_dados_cnae_secundario_filt.csv' WITH ( FIELDTERMINATOR = '#', FIRSTROW = 2, )
– RxT