SQL: convert backup file from copy format to insert format
NickName:takeshin Ask DateTime:2010-05-13T21:20:57

SQL: convert backup file from copy format to insert format

I have a PostgreSQL backup made with PHPPgadmin using Export > Copy (instead Copy > SQL which is actually what I need).

File contains entries like this:

COPY tablename(id, field) FROM stdin;
...

How to convert this file to SQL format?

INSERT INTO tablename...

I want to use Pgadmin to to import this file using execute SQL command.

Copyright Notice:Content Author:「takeshin」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2827072/sql-convert-backup-file-from-copy-format-to-insert-format

Answers
Steffen 2013-09-20T07:25:24

I don't know a way or a tool which can convert \"COPY\" into \"INSERT\" statements.\n\nBut with the \"pg_dump\" command line tool you can create a DB dump file with \"INSERT\" instead of \"COPY\" statements. Simple use the \"--column-inserts\" argument (may be \"--inserts\" is also ok for you).\n\nEDIT:\n\nWhat you can do is:\n\n\nCreate a new postgres database on your local machine\nImport your DB dump file into the new created database\n\npsql -U <dbuser> <database> < dump.sql\n\nAnd create a postgres dump with \"--column-inserts\" from that database\n\npg_dump --column-inserts -U <dbuser> <database> > dumpWithInserts.sql\n\n",


fredz 2017-01-16T13:19:03

I've wrote a tool to convert copy-from dump to inserts dump :\n\nhttps://github.com/freddez/pg-dump2insert\n\nYou can use it to load a dump in another database or search for specific deleted values for example.",


leonbloy 2010-05-13T16:31:22

You can't convert that to SQL format (at least not straightforwardly).\n\nIf you can't re export, then the simpler option is to bypass phpPgadmin,\nlogin in your server and run something like \n\ncat [yourdump.sql] | psql [your connections args]\n\n\nIf you don't have shell access to your server, you might try you upload the file (via SFTP or FTP) and load it thorugh phpPgAdmin with \"COPY <table> FROM <path_to_file_on_server>\". But if there are many tables, you must (I believe) split the file and do it one at a time.\n\nhttp://phppgadmin.sourceforge.net/?page=faq",


Dmitry Ziyatdinov 2015-05-26T16:12:34

You can try COPY FROM PROGRAM syntax, available since PostgreSQL 9.3\n\nFor example:\nCOPY tbl_customers (id, address_id, insurance_id, fullname, secret_code ...) FROM PROGRAM 'echo \"1 2 \\N Ivan Ivanov 42 ...\"'",


More about “SQL: convert backup file from copy format to insert format” related questions

SQL: convert backup file from copy format to insert format

I have a PostgreSQL backup made with PHPPgadmin using Export > Copy (instead Copy > SQL which is actually what I need). File contains entries like this: COPY tablename(id, field) FROM stdin; ... ...

Show Detail

Backup mysql database in ms-access format using php

i am using mysqldump to backup the database available in MySQL using the PHP to the sql dump format which creates the .SQL file. if i want to view / check the backup file then i have to import it f...

Show Detail

SQL format date from insert

I have a dbf file converted to SQL. The date format for sql is YYYY-MM-DD. but the dbf date format is MM-DD-YY. Is there a sql function that will return a YYYY-MM-DD format ? like INSERT date_in V...

Show Detail

SQL copy data from another table of database with specified column format on destination

in current column of our table we save data in JSON format such as : { &quot;ru&quot;:&quot;text&quot;, &quot;en&quot;:&quot;text&quot; } and into another column of table we have a simple stri..

Show Detail

number format in SQL INSERT

Hi guys i need some support on number format in SQL INSERT INTO i need to convert {$data['rank']['points']} as number format, so that instead of it showing as 5467389 it converts it to 54,673,89 the

Show Detail

How to insert date( in different format ) coming from a CSV file to MySQL

I'm inserting a row coming from a CSV file into my SQL database. The birthdate column that I have on the CSV file has a date format like this 10/29/1992, it seems that the Google Sheets that I use to

Show Detail

Backup SQL Server database using WITH FORMAT

I'm having a bit of trouble understanding the purpose of the WITH FORMAT option of the SQL Server BACKUP DATABASE command. Using the MSDN example: USE AdventureWorks2012; GO BACKUP DATABASE

Show Detail

How to backup a table or entire database by query as SQL format

I know that we can backup a table by query as CSV format like this: SELECT * FROM db.table INTO OUTFILE 'C:/backup' CHARACTER SET UTF8 FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATE

Show Detail

What is a .backup format?

Some time ago i was working on a database made in Postgresql, i learned to make backups by exporting the database to a .sql file, but i recently found a .backup file with the name of the database t...

Show Detail

Convert inserted string in date format of sql

is it possible to if i am inserted '15-05-2018 16:09:21' in this format or any other format like '05-15-2018 16:09:21' etc. and at insert time in table it will convert in '2018-05-15 16:09:21' in t...

Show Detail