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 6954047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:33:21+00:00 2026-05-27T14:33:21+00:00

Possible Duplicate: Dynamically-generated table-name in PostgreSQL COPY command I am using the copy to

  • 0

Possible Duplicate:
Dynamically-generated table-name in PostgreSQL COPY command

I am using the copy to file function in postgresql, which works fine.

Current code looks like this

COPY table 
TO 'filename.txt'

This works fine, but I would like to add date to the filename.

So I tried

COPY table 
TO 'filename' || CURRENT_DATE || '.txt'

without success.

I have also tried putting it in a query

COPY table 
TO (select 'filename' || CURRENT_DATE || '.txt')

Anyone willing to help me out?

EDIT

I think the problem is, that the function COPY TO expects a string, as the destination. And I put in a query, returning a resultset…

  • 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-27T14:33:21+00:00Added an answer on May 27, 2026 at 2:33 pm

    As you suspected, the problem is that PostgreSQL is that expecting a string-literal filename, in the form 'filename' or E'filename', rather than an expression (such as a subquery) that would generate a filename as varchar or text or whatnot. So far as I’m aware, the only work-around is to dynamically generate the entire COPY statement, and run it in PL/pgSQL using the EXECUTE command. You can create a function like this:

    CREATE OR REPLACE FUNCTION copy_table_to_file(filename TEXT) RETURNS VOID AS
    $$
      BEGIN
        EXECUTE 'COPY table TO ''' || filename || '''';
      END;
    $$ LANGUAGE 'plpgsql';
    

    and then invoke it like this:

    SELECT copy_table_to_file('filename' || CURRENT_DATE || '.txt');
    

    A few notes:

    • If desired, you can make the table-name be a parameter as well.
    • As you can see from the above-linked documentation, the EXECUTE command supports a USING clause to pass in data; but I’ve just tested, and it seems that something like EXECUTE 'COPY table TO $1' USING filename; does not work. I assume that this is for more or less the same reason as your original question: the filename has to be specified as a string-literal, not as a substituted parameter. So, be careful with the filename you pass into the function, and make sure it never contains any single-quotes or backslashes or whatnot, or else you’ll accidentally perform a SQL injection attack on yourself.
    • You may be interested in this previous, related question: Dynamically-generated table-name in PostgreSQL COPY command
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I get the name of function inside a JavaScript function?
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Dynamically loading an external CSS file I'm trying to create a dynamic
Possible Duplicate: What characters are allowed in C# class name? I'm using edmgen to
Possible Duplicate: Getting an issue while checking the dynamically generated checkbox through list view
Possible Duplicate: Use javascript variable in object name I am using CKeditor as a
Possible Duplicate: Dynamically retrieving current method's name Obj-C introspection: How can a method reference
Possible Duplicate: Change CSS Dynamically I need to change the height of a div
Possible Duplicate: How can I access local scope dynamically in javascript? Hi all. We
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

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.