5

I would like to important a file into my Postgresql system(specificly RedShift). I have found a arguement for copy that allows importing a gzip file. But the provider for the data I am trying to include in my system only produces the data in a .zip. Any built in postgres commands for opening a .zip?

4 Answers 4

3

From within Postgres:

COPY table_name FROM PROGRAM 'unzip -p input.csv.zip' DELIMITER ',';

From the man page for unzip -p:

-p     extract files to pipe (stdout).  Nothing but the file data is sent to stdout, and the files are always extracted  in  binary
       format, just as they are stored (no conversions).
Sign up to request clarification or add additional context in comments.

Comments

2

Can you just do something like

unzip -c myfile.zip | gzip myfile.gz

Easy enough to automate if you have enough files.

2 Comments

File is not being stored locally and database is not local.
Ug. Well that makes it tough because I don't think amzn has any option for that. Can you get access to exec psql on the server where file is stored and connect that way? Otherwise might have to send the file through a middle-man server where you can.
1

This might only work when loading redshift from S3, but you can actually just include a "gzip" flag when copying data to redshift tables, as described here:

This is the format that works for me if my s3 bucket contains a gzipped .csv.

copy <table> from 's3://mybucket/<foldername> '<aws-auth-args>' delimiter ',' gzip;

Comments

1
unzip -c /path/to/.zip | psql -U user

The 'user' must be have super user right else you will get a

ERROR:  must be superuser to COPY to or from a file

To learn more about this see here.

Basically this command is used in handling large databases

1 Comment

Alternatively, you can also pipe into unzip (or gunzip in my case). Consider cat /path/to/.zip | unzip -c | psql -U user (simplified for your case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.