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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:05:58+00:00 2026-06-14T11:05:58+00:00

using the COPY command in postgresql 8.4 to export data of type bytea, this

  • 0

using the COPY command in postgresql 8.4 to export data of type bytea, this would work fine for a single record:

copy (SELECT encode(test_column, 'hex') FROM test_table LIMIT 1) TO '/home/user/file.hex'

after that, i use the xxd command to obtain the appropriate file type.
sorry in advance if this is a very noob question, but is it possible to export all the records into file-1,file-2…file-n? what’s the right syntax or script? i cant seem to find it in the man of postgresql copy.
for example, this is a bash script for a single row:

#!/bin/bash

psql \  
    -P t \  
    -P format=unaligned \  
    -X \  
    -U myuser \  
    -h myhost \  
    -c "select my_bytea_col from my_table where id=1" \  
    mydb \  
| xxd -r -p > dump.txt  

however, i need to execute it on all ids and have them saved under different files each such as file1.txt, file2.txt, file3.txt.. etc..
what could also be very interesting is the ability to use another column’s data (such as id) to name the resulting hex file accordingly.

  • 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-06-14T11:05:59+00:00Added an answer on June 14, 2026 at 11:05 am

    I’d use a scripting language with direct PostgreSQL support for this.

    This does the job quite nicely given a database named regress containing a table called files with columns fileid (an integer) and filedata (a bytea). It writes the data to files in the directory specified by the outdir variable, in files named according to a pattern like {filenameprefix}{fileid}{filenamesuffix}.

    #!/usr/bin/env python3
    import os
    import sys
    import psycopg2
    
    outdir = "files"
    filenameprefix = "f"
    filenamesuffix = ""
    
    def main():
            os.makedirs(outdir, exist_ok=True)
            conn = psycopg2.connect("dbname=regress")
            curs = conn.cursor();
            curs.execute("SELECT fileid, filedata FROM files;")
            for (fileid, filedata) in curs:
                fn = filenameprefix+str(fileid)+filenamesuffix
                f = open(os.path.join(outdir, fn), "wb")
                f.write(filedata)
                f.close()
            conn.close();
    
    if __name__ == '__main__':
            main()
    

    I used this code to create the test environment referred to in the above script:

    #!/usr/bin/env python3
    import psycopg2
    conn = psycopg2.connect("dbname=regress")
    curs = conn.cursor()
    curs.execute('CREATE TABLE files(fileid serial primary key, filedata bytea);')
    for i in range(1,10)
        curs.execute('INSERT INTO  files(filedata) VALUES (%s);', (psycopg2.Binary(open('/dev/urandom','rb').read(1024)),))
    

    Edit: To make it the same as the naming you used in your question, which I just assumed was generic anonymized names, change:

    • regress -> whatever your database name is
    • files -> my_table
    • fileid -> id
    • filedata -> my_bytea_col

    The equivalent to this script can be done in bash, it’s just cumbersome. You’d need to use a co-process to do it in a transaction-safe way. You’d need to create a psql co-process and open a SERIALIZABLE transaction in it, then SELECT fileid FROM files into a shell variable. Loop over the shell variable and for each ID, SELECT filedata FROM files into the xxd pipe to a filename created with string interpolation on the ID.

    IMO: Not worth the hassle, use a scripting language that gives you direct PostgreSQL access.

    If you don’t need it transaction safe it’s a little easier; just fetch the IDs in one psql invocation, then the file data in subsequent ones. It’ll be slower and won’t be transaction safe but it’ll be easier.

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

Sidebar

Related Questions

I have a PostgreSQL backup made with PHPPgadmin using Export > Copy (instead Copy
I am using Postgres 8.4.4 copy, http://www.postgresql.org/docs/8.4/static/sql-copy.html , to import CSV data into my
I want to copy data from file to PostgreSQL DB using JDBC. I was
Using the COPY command ( http://www.postgresql.org/docs/9.1/static/sql-copy.html ) I get each CHAR column filled up
Possible Duplicate: Dynamically-generated table-name in PostgreSQL COPY command I am using the copy to
I am implementing cp(file copy) command using mmap(). For that I mapped the source
I have been using this copy method for quite a while, in lots of
I have a script that I am using to copy data from a table
I am concatenating video files of mpg format using the 'Copy' command in command
I cannot create working copy from command line using svn utility if resulting full

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.