FAQ

[PostgreSQL] Copy command to handle view for my export requirement

Siva Palanisamy
Aug 11, 2011 at 7:12 am
Hi All,

I understand that copy and \copy commands in PostgreSQL work only for tables. I want it to export the data from varies tables. Instead, I can create a view for the list of tables. Can the copy or \copy commands be utilized to operate on views directly? Please let me know on this.

Thanks and Regards,
Siva.


________________________________
::DISCLAIMER::
-----------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
received this email in error please delete it and notify the sender immediately. Before opening any mail and
attachments please check them for viruses and defect.

-----------------------------------------------------------------------------------------------------------------------
reply

Search Discussions

9 responses

  • Andreas Kretschmer at Aug 11, 2011 at 8:53 am

    Siva Palanisamy wrote:

    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
  • Siva Palanisamy at Aug 11, 2011 at 11:18 am
    Hi Andreas,

    I tried the command as below. It failed. Please correct me.

    \copy (select * from view1) to '/sample.csv' delimiters ',' csv header;
    ERROR: \copy: parse error at "select"

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Andreas Kretschmer
    Sent: Thursday, August 11, 2011 2:23 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Siva Palanisamy wrote:
    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    ::DISCLAIMER::
    -----------------------------------------------------------------------------------------------------------------------

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
    It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
    this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
    Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
    this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
    received this email in error please delete it and notify the sender immediately. Before opening any mail and
    attachments please check them for viruses and defect.

    -----------------------------------------------------------------------------------------------------------------------
  • Siva Palanisamy at Aug 11, 2011 at 11:22 am
    Hi Andreas,

    FYI, I am using PostgreSQL 8.1.4.

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Siva Palanisamy
    Sent: Thursday, August 11, 2011 4:48 PM
    To: Andreas Kretschmer; pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Hi Andreas,

    I tried the command as below. It failed. Please correct me.

    \copy (select * from view1) to '/sample.csv' delimiters ',' csv header;
    ERROR: \copy: parse error at "select"

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Andreas Kretschmer
    Sent: Thursday, August 11, 2011 2:23 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Siva Palanisamy wrote:
    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    ::DISCLAIMER::
    -----------------------------------------------------------------------------------------------------------------------

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
    It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
    this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
    Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
    this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
    received this email in error please delete it and notify the sender immediately. Before opening any mail and
    attachments please check them for viruses and defect.

    -----------------------------------------------------------------------------------------------------------------------

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general
  • Raghavendra at Aug 11, 2011 at 11:25 am
    Nope, you need to be in latest version as Andreas said.
    ---
    Regards,
    Raghavendra
    EnterpriseDB Corporation
    Blog: http://raghavt.blogspot.com/


    On Thu, Aug 11, 2011 at 4:51 PM, Siva Palanisamy wrote:

    Hi Andreas,

    FYI, I am using PostgreSQL 8.1.4.

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org [mailto:
    pgsql-general-owner@postgresql.org] On Behalf Of Siva Palanisamy
    Sent: Thursday, August 11, 2011 4:48 PM
    To: Andreas Kretschmer; pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export
    requirement

    Hi Andreas,

    I tried the command as below. It failed. Please correct me.

    \copy (select * from view1) to '/sample.csv' delimiters ',' csv header;
    ERROR: \copy: parse error at "select"

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org [mailto:
    pgsql-general-owner@postgresql.org] On Behalf Of Andreas Kretschmer
    Sent: Thursday, August 11, 2011 2:23 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export
    requirement

    Siva Palanisamy wrote:
    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    ::DISCLAIMER::

    -----------------------------------------------------------------------------------------------------------------------

    The contents of this e-mail and any attachment(s) are confidential and
    intended for the named recipient(s) only.
    It shall not attach any liability on the originator or HCL or its
    affiliates. Any views or opinions presented in
    this email are solely those of the author and may not necessarily reflect
    the opinions of HCL or its affiliates.
    Any form of reproduction, dissemination, copying, disclosure, modification,
    distribution and / or publication of
    this message without the prior written consent of the author of this e-mail
    is strictly prohibited. If you have
    received this email in error please delete it and notify the sender
    immediately. Before opening any mail and
    attachments please check them for viruses and defect.


    -----------------------------------------------------------------------------------------------------------------------

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general
  • Boszormenyi Zoltan at Aug 11, 2011 at 11:40 am
    Hi,

    COPY (SELECT ...) appeared in 8.2.x so you need to upgrade.

    Best regards,
    Zoltán Böszörményi

    2011-08-11 13:21 keltezéssel, Siva Palanisamy írta:
    Hi Andreas,

    FYI, I am using PostgreSQL 8.1.4.

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Siva Palanisamy
    Sent: Thursday, August 11, 2011 4:48 PM
    To: Andreas Kretschmer; pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Hi Andreas,

    I tried the command as below. It failed. Please correct me.

    \copy (select * from view1) to '/sample.csv' delimiters ',' csv header;
    ERROR: \copy: parse error at "select"

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Andreas Kretschmer
    Sent: Thursday, August 11, 2011 2:23 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Siva Palanisamy wrote:
    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    ::DISCLAIMER::
    -----------------------------------------------------------------------------------------------------------------------

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
    It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
    this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
    Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
    this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
    received this email in error please delete it and notify the sender immediately. Before opening any mail and
    attachments please check them for viruses and defect.

    -----------------------------------------------------------------------------------------------------------------------

    --
    ----------------------------------
    Zoltán Böszörményi
    Cybertec Schönig & Schönig GmbH
    Gröhrmühlgasse 26
    A-2700 Wiener Neustadt, Austria
    Web: http://www.postgresql-support.de
    http://www.postgresql.at/
  • Siva Palanisamy at Aug 11, 2011 at 11:56 am
    Hi,

    I have installed Windows version of Postgres 9.0.4 in my windows machine to test the new copy command as detailed in the below e-mails. When I run the command in SQL Editor, I got permission error. But I am running as an administrator.

    COMMAND: copy (select * from employee) to 'C:/emp.csv'
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    ********** Error **********
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    SQL state: 42501

    COMMAND: \copy (select * from employee) to 'C:/emp.csv'
    ERROR: syntax error at or near "\"
    LINE 1: \copy (select * from employee) to 'C:/emp.csv'
    ^
    ********** Error **********
    ERROR: syntax error at or near "\"
    SQL state: 42601

    Please correct me where I am going wrong. FYI, I am running under the administrator accounts of both Windows Login and PostgreSQL.

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Boszormenyi Zoltan
    Sent: Thursday, August 11, 2011 5:11 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Hi,

    COPY (SELECT ...) appeared in 8.2.x so you need to upgrade.

    Best regards,
    Zoltán Böszörményi

    2011-08-11 13:21 keltezéssel, Siva Palanisamy írta:
    Hi Andreas,

    FYI, I am using PostgreSQL 8.1.4.

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Siva Palanisamy
    Sent: Thursday, August 11, 2011 4:48 PM
    To: Andreas Kretschmer; pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Hi Andreas,

    I tried the command as below. It failed. Please correct me.

    \copy (select * from view1) to '/sample.csv' delimiters ',' csv header;
    ERROR: \copy: parse error at "select"

    Thanks and Regards,
    Siva.


    -----Original Message-----
    From: pgsql-general-owner@postgresql.org On Behalf Of Andreas Kretschmer
    Sent: Thursday, August 11, 2011 2:23 PM
    To: pgsql-general@postgresql.org
    Subject: Re: [GENERAL] Copy command to handle view for my export requirement

    Siva Palanisamy wrote:
    Hi All,



    I understand that copy and \copy commands in PostgreSQL work only for tables. I
    want it to export the data from varies tables. Instead, I can create a view for
    the list of tables. Can the copy or \copy commands be utilized to operate on
    views directly? Please let me know on this.
    Sure, you can do that (with recent versions) with:

    copy (select * from your_view) to ...


    Andreas
    --
    Really, I'm not out to destroy Microsoft. That will just be a completely
    unintentional side effect. (Linus Torvalds)
    "If I was god, I would recompile penguin with --enable-fly." (unknown)
    Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general

    ::DISCLAIMER::
    -----------------------------------------------------------------------------------------------------------------------

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
    It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
    this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
    Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
    this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
    received this email in error please delete it and notify the sender immediately. Before opening any mail and
    attachments please check them for viruses and defect.

    -----------------------------------------------------------------------------------------------------------------------

    --
    ----------------------------------
    Zoltán Böszörményi
    Cybertec Schönig & Schönig GmbH
    Gröhrmühlgasse 26
    A-2700 Wiener Neustadt, Austria
    Web: http://www.postgresql-support.de
    http://www.postgresql.at/


    --
    Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
    To make changes to your subscription:
    http://www.postgresql.org/mailpref/pgsql-general
  • Raghavendra at Aug 11, 2011 at 12:04 pm

    COMMAND: copy (select * from employee) to 'C:/emp.csv'
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    ********** Error **********
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    SQL state: 42501

    COMMAND: \copy (select * from employee) to 'C:/emp.csv'
    ERROR: syntax error at or near "\"
    LINE 1: \copy (select * from employee) to 'C:/emp.csv'
    ^
    ********** Error **********
    ERROR: syntax error at or near "\"
    SQL state: 42601

    Please correct me where I am going wrong. FYI, I am running under the
    administrator accounts of both Windows Login and PostgreSQL.
    Two things,
    1. you need to have a proper permissions where the .csv file creating.
    2. In windows you need to use as below
    postgres=#\copy (select * from employee) to 'C:\\emp.sql'

    Regards,
    Raghavendra
    EnterpriseDB Corporation
    Blog: http://raghavt.blogspot.com/
  • Craig Ringer at Aug 11, 2011 at 12:46 pm

    On 11/08/2011 7:56 PM, Siva Palanisamy wrote:
    FYI, I am using PostgreSQL 8.1.4.
    Argh, ogod why?!?!?!

    That version is *totally* unsupported on Windows. Not only that, but
    you're running an ancient point-release - you are missing *19* patch
    releases worth of bug fixes. The latest point-release is 8.1.23 !

    Here is a list of all the fixes you are missing out on:

    http://www.postgresql.org/docs/8.1/static/release.html
    I have installed Windows version of Postgres 9.0.4 in my windows machine to test the new copy command as detailed in the below e-mails. When I run the command in SQL Editor, I got permission error. But I am running as an administrator.

    COMMAND: copy (select * from employee) to 'C:/emp.csv'
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    ********** Error **********
    ERROR: could not open file "C:/emp.csv" for writing: Permission denied
    SQL state: 42501
    The COPY command (as distinct from \copy) runs on the server-side so it
    has the permissions of the "postgres" user. You must save the file
    somewhere the "postgres" user as write access. Either create a folder
    and give "full control" to the user "postgres", or write the export
    within the existing postgresql data directory.
    COMMAND: \copy (select * from employee) to 'C:/emp.csv'
    ERROR: syntax error at or near "\"
    LINE 1: \copy (select * from employee) to 'C:/emp.csv'
    You are not using psql. "\copy" is a psql command. I don't think it's
    supported by PgAdmin III, though I could be wrong.



    --
    Craig Ringer
  • Raghavendra at Aug 11, 2011 at 1:16 pm


    You are not using psql. "\copy" is a psql command. I don't think it's
    supported by PgAdmin III, though I could be wrong.
    Right, '\copy' is not supported in PgAdmin III.

    --Raghav

Related Discussions