0

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

7
  • 1
    \copy is only available in the client program psql. Commented Jun 27, 2024 at 16:52
  • @AdrianKlaver there is access from pgAdmin 4 to psql from the toolbar, and copy command can be executed as a query without the backslash. Commented Jun 27, 2024 at 17:13
  • stackoverflow.com/questions/53777508/… Commented Jun 27, 2024 at 17:15
  • How are you executing copy command? The only error that I see is in "with" options, reserved word "with" must be written just once, check out postgresql.org/docs/current/sql-copy.html, other than that your code runs without a problem. Commented Jun 27, 2024 at 17:19
  • @PepeNO. 1) The error for \copy indicated it was not being run in psql. 2) COPY needs server user access to the file and its parent directories. 3) This copy estabelecimento join ... is not going two work as a) There is no SELECT. and b) There are no () around the query. 4) As to WITH see COPY Compatibility section. Commented Jun 27, 2024 at 17:30

1 Answer 1

0

Assuming select query is correct, use:

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 '/Users/xyz/workspace/xyz/estabelecimento.csv' WITH(FORMAT csv, DELIMITER ';', HEADER true);

Replace filepath to a valid path in your system. I've already tried in pgadmin, let me know if it works .

Sign up to request clarification or add additional context in comments.

1 Comment

This creates a file on the database server, if you have the superuser permissions to do so. When your database server is remote, you get the file on that remote location. Not on your local PC

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.