Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 393343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:14:52+00:00 2026-05-12T16:14:52+00:00

What is the easiest way to save PL/pgSQL output from a PostgreSQL database to

  • 0

What is the easiest way to save PL/pgSQL output from a PostgreSQL database to a CSV file?

I’m using PostgreSQL 8.4 with pgAdmin III and PSQL plugin where I run queries from.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:14:53+00:00Added an answer on May 12, 2026 at 4:14 pm

    Do you want the resulting file on the server, or on the client?

    Server side

    If you want something easy to re-use or automate, you can use Postgresql’s built in COPY command. e.g.

    Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER;
    

    This approach runs entirely on the remote server – it can’t write to your local PC. It also needs to be run as a Postgres "superuser" (normally called "root") because Postgres can’t stop it doing nasty things with that machine’s local filesystem.

    That doesn’t actually mean you have to be connected as a superuser (automating that would be a security risk of a different kind), because you can use the SECURITY DEFINER option to CREATE FUNCTION to make a function which runs as though you were a superuser.

    The crucial part is that your function is there to perform additional checks, not just by-pass the security – so you could write a function which exports the exact data you need, or you could write something which can accept various options as long as they meet a strict whitelist. You need to check two things:

    1. Which files should the user be allowed to read/write on disk? This might be a particular directory, for instance, and the filename might have to have a suitable prefix or extension.
    2. Which tables should the user be able to read/write in the database? This would normally be defined by GRANTs in the database, but the function is now running as a superuser, so tables which would normally be "out of bounds" will be fully accessible. You probably don’t want to let someone invoke your function and add rows on the end of your “users” table…

    I’ve written a blog post expanding on this approach, including some examples of functions that export (or import) files and tables meeting strict conditions.


    Client side

    The other approach is to do the file handling on the client side, i.e. in your application or script. The Postgres server doesn’t need to know what file you’re copying to, it just spits out the data and the client puts it somewhere.

    The underlying syntax for this is the COPY TO STDOUT command, and graphical tools like pgAdmin will wrap it for you in a nice dialog.

    The psql command-line client has a special "meta-command" called \copy, which takes all the same options as the "real" COPY, but is run inside the client:

    \copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER
    

    Note that there is no terminating ;, because meta-commands are terminated by newline, unlike SQL commands.

    From the docs:

    Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.

    Your application programming language may also have support for pushing or fetching the data, but you cannot generally use COPY FROM STDIN/TO STDOUT within a standard SQL statement, because there is no way of connecting the input/output stream. PHP’s PostgreSQL handler (not PDO) includes very basic pg_copy_from and pg_copy_to functions which copy to/from a PHP array, which may not be efficient for large data sets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer With a C-style string, you can just set the character… May 16, 2026 at 5:13 pm
  • Editorial Team
    Editorial Team added an answer timeIntervalSince1970 returns the number of seconds (in a double) since… May 16, 2026 at 5:13 pm
  • Editorial Team
    Editorial Team added an answer Where were you expecting to store all this data? List<T>… May 16, 2026 at 5:13 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

What is the easiest way to save UIView's representation to file? My solution is,
What's the easiest way to import a CSV into a database via web application?
What is the easiest way to save a couple of variables in an iPhone
What's the easiest way to save the install log that is displayed in NSIS?
What is the easiest way to convert and save a 32-bit Bitmap to a
Normally the easiest way to debug is using printf . What can I do
What is the easiest way to write content of a dictionary into a file.
What's the easiest way to get all input fields inside a div without using
What is the easiest way to convert a Rectangle to a RectangleF in .NET?
What's the easiest way to draw a hex grid algorithmically? How should I present

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.