I need to use the copy function with inner join from more than one table with where.
Postgresql if pgadmin 4
When I do this query, it returns the data I need
select *
from estabelecimento
join empresa on estabelecimento.cnpj_basico = empresa.cnpj_basico
join simples on estabelecimento.cnpj_basico = simples.cnpj_basico
join socios on estabelecimento.cnpj_basico = socios.cnpj_basico
where cnae_fiscal_principal = 8630503
limit 20000
But now I need to export it to a csv file
copy (select * from estabelecimento
join empresa on estabelecimento.cnpj_basico = empresa.cnpj_basico
join simples on estabelecimento.cnpj_basico = simples.cnpj_basico
join socios on estabelecimento.cnpj_basico = socios.cnpj_basico
where cnae_fiscal_principal = 8630503
limit 20000)
to 'C:\Dados_RFB\exportaçãocsv\exp.csv'
WITH DELIMITER ';'
with csv header
Messages:
ERROR: erro de sintaxe em ou próximo a ""
LINE 1: \copy (select * from estabelecimento
^ERRO: erro de sintaxe em ou próximo a ""
SQL state: 42601
Character: 1
Can you help me solve this?
I've already tried:
copy estabelecimento
join empresa on estabelecimento.cnpj_basico = empresa.cnpj_basico
join simples on estabelecimento.cnpj_basico = simples.cnpj_basico
join socios on estabelecimento.cnpj_basico = socios.cnpj_basico
where cnae_fiscal_principal = 8630503
limit 20000
to 'C:\Dados_RFB\exportaçãocsv\exp.csv'
WITH DELIMITER ';'
with csv header
ERROR: erro de sintaxe em ou próximo a ""
LINE 1: \copy estabelecimento
^ERRO: erro de sintaxe em ou próximo a ""
SQL state: 42601
Character: 1
\copyis only available in the client program psql.\copyindicated it was not being run inpsql. 2)COPYneeds server user access to the file and its parent directories. 3) Thiscopy estabelecimento join ...is not going two work as a) There is no SELECT. and b) There are no()around the query. 4) As toWITHsee COPY Compatibility section.