%3CLINGO-SUB%20id%3D%22lingo-sub-1561266%22%20slang%3D%22en-US%22%3EMoving%20data%20with%20PostgreSQL%20COPY%20and%20%5CCOPY%20commands%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1561266%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20you%20want%20to%20move%20data%20into%20your%20PostgreSQL%20database%2C%20there%20are%20a%20few%20options%20available%20like%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Flogin-and-permission-tips-and-tricks-in-using-pg-dump-and-pg%2Fba-p%2F864167%22%20target%3D%22_blank%22%3Epg_dump%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fcopy-data-from-azure-blob-to-azure-database-for-postgresql-using%2Fba-p%2F1022220%22%20target%3D%22_blank%22%3EAzure%20Data%20Factory%3C%2FA%3E.%20The%20method%20you%20pick%20depends%20on%20the%20scenario%20you%20want%20to%20enable.%20Today%2C%20we%E2%80%99re%20exploring%20three%20scenarios%20where%20you%20can%20consider%20using%20PostgreSQL%E2%80%99s%20COPY%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%E2%80%9C%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fsql-copy.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ECOPY%3C%2FA%3E%20is%20the%20Postgres%20method%20of%20data-loading.%20Postgres's%20COPY%20comes%20in%20two%20separate%20variants%2C%20COPY%20and%20%5CCOPY%3A%20COPY%20is%20server%20based%2C%20%5CCOPY%20is%20client%20based.%E2%80%9D%20-%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwiki.postgresql.org%2Fwiki%2FCOPY%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EThe%20PostgreSQL%20Wiki%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBoth%20versions%20of%20COPY%20move%20data%20from%20a%20file%20to%20a%20Postgres%20table.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20data-unlink%3D%22true%22%3EThe%20server%20based%20COPY%20command%20has%20limited%20file%20access%20and%20user%20permissions%2C%20and%20isn%E2%80%99t%20available%20for%20use%20on%20Azure%20Database%20for%20PostgreSQL.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%5CCOPY%20runs%20COPY%20internally%2C%20but%20with%20expanded%20permissions%20and%20file%20access.%20The%20source%20file%20does%20not%20have%20to%20exist%20on%20the%20same%20machine%20as%20the%20Postgres%20instance%20if%20you%20use%20%5CCOPY.%20You%20can%20even%20use%20both%20absolute%20and%20relative%20paths.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%5CCOPY%20is%20a%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fapp-psql.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Epsql%3C%2FA%3E%20feature.%20You%20need%20to%20have%20the%20psql%20application%20to%20be%20able%20to%20use%20%5CCOPY.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EToday%2C%20we%E2%80%99ll%20use%20%5CCOPY%20to%20enable%20these%20three%20scenarios%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EMigrating%20data%20from%20Postgres%20to%20Postgres%3C%2FLI%3E%0A%3CLI%3EIngesting%20from%20http%20data%20sources%3C%2FLI%3E%0A%3CLI%3EMoving%20data%20from%20other%20database%20engines%2C%20like%20MySQL%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH1%20id%3D%22toc-hId-500574668%22%20id%3D%22toc-hId-500574668%22%3E%26nbsp%3B%3C%2FH1%3E%0A%3CH1%20id%3D%22toc-hId--1306879795%22%20id%3D%22toc-hId--1306879795%22%3EMigrating%20data%20from%20Postgres%20to%20Postgres%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20our%20first%20scenario%2C%20we%E2%80%99ll%20do%20a%20data%20migration%20between%20two%20PostgreSQL%20servers.%20We%E2%80%99ll%20first%20move%20the%20data%20with%20pg_dump%2C%20with%20and%20without%20parallelism.%20Then%20we%E2%80%99ll%20compare%20the%20performance%20using%20%5CCOPY.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThese%20are%20the%20advantages%20and%20disadvantages%20I%20expect%20to%20see%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%3CU%3EAdvantages%3C%2FU%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%5CCOPY%20command%20goes%20through%20pipe%2C%20so%20no%20space%20required%20on%20client%20side%3C%2FLI%3E%0A%3CLI%3EPotential%20for%20increased%20performance%20with%20parallelism%2C%20compared%20to%20pg_dump%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CEM%3E%3CU%3EDisadvantages%3C%2FU%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%5CCOPY%20method%20does%20not%20migrate%20indexes%2C%20foreign%20keys%2C%20and%20other%20similar%20objects.%20You%E2%80%99ll%20have%20to%20take%20additional%20steps%20to%20move%20those%20objects.%26nbsp%3B%20Alternatively%2C%20you%20can%20consider%20using%20a%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fdms%2Ftutorial-postgresql-azure-postgresql-online-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Emigration%20service%3C%2FA%3E.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20this%20example%2C%20I%20will%20be%20migrating%20from%20an%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fpostgresql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%3C%2FA%3E%26nbsp%3BBasic%20tier%20single%20Server%20to%20a%20General%20Purpose%20tier%20single%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--616318321%22%20id%3D%22toc-hId--616318321%22%3EPrepare%20psql%20connection%20string%20scripts%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20I%E2%80%99ll%20store%20the%20psql%20connection%20strings%20to%20my%20source%20and%20destination%20servers%20in%20scripts.%20That%20way%20I%20can%20call%20those%20scripts%20when%20I%20want%20to%20connect.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Epgsource.sh%20%3D%20bash%20script%20with%20psql%20and%20connection%20string%20to%20source%20%60tests%60%20database%3C%2FP%3E%0A%3CP%3Epgdestination.sh%20%3D%20bash%20script%20with%20psql%20and%20connection%20string%20to%20destination%20%60tests%60%20database%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Ecat%20%26lt%3B%3CEOF%3E%20pgsource.sh%0APGPASSWORD%3D%3CSOURCEPASSWORD%3E%20psql%20-t%20-h%20%3CSOURCESERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CSOURCESERVERNAME%3E%20tests%0AEOF%0Achmod%20%2Bx%20pgsource.sh%0A%0Acat%20%26lt%3B%3CEOF%3E%20pgdestination.sh%0APGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-t%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%0AEOF%0Achmod%20%2Bx%20pgdestination.sh%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FEOF%3E%3C%2FSOURCESERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FSOURCESERVERNAME%3E%3C%2FSOURCEPASSWORD%3E%3C%2FEOF%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMy%20source%20database%20has%2020%20tables%20each%20with%201%20million%20auto-generated%20rows.%20There%20are%20no%20indexes%20or%20foreign%20keys%20in%20this%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1871194512%22%20id%3D%22toc-hId-1871194512%22%3ETest%20full%20restoration%20with%20pg_dump%2Fpg_restore%20(no%20parallelism)%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInternally%2C%20pg_dump%20and%20pg_restore%20uses%20the%20COPY%20command%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Etime%20PGPASSWORD%3D%3CSOURCEPASSWORD%3E%20pg_dump%20-C%20-Fc%20-h%20%3CSOURCESERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CSOURCESERVERNAME%3E%20tests%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20pg_restore%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20-C%20-c%20-d%20postgres%0A%0Areal%26nbsp%3B%26nbsp%3B%26nbsp%3B%206m15.310s%0Auser%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m22.976s%0Asys%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m9.393s%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FSOURCESERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FSOURCESERVERNAME%3E%3C%2FSOURCEPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Total%20Time%3A%206m15sec%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-63740049%22%20id%3D%22toc-hId-63740049%22%3ETest%20full%20restoration%20with%20pg_dump%2Fpg_restore%20(parallel)%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%E2%80%99ll%20need%20to%20use%20the%20directory%20format%20export%2C%20and%20dump%20to%20regular%20files.%20(See%20the%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fapp-pgdump.html)%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Epg_dump%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fapp-pgrestore.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Epg_restore%3C%2FA%3E%20documentation%20to%20learn%20about%20format%20options).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Etime%20PGPASSWORD%3D%3CSOURCEPASSWORD%3E%20pg_dump%20-C%20-Fd%20-h%20%3CSOURCESERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CSOURCESERVERNAME%3E%20tests%20-j%204%20-f%20testsdir%0A%0Areal%26nbsp%3B%26nbsp%3B%26nbsp%3B%203m23.004s%0Auser%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m20.291s%0Asys%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m13.888s%0A%0Atime%20PGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20pg_restore%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20-C%20-c%20-d%20postgres%20-j%204%20testsdir%0A%0Areal%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m56.175s%0Auser%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m1.671s%0Asys%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m0.478s%0A%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FSOURCESERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FSOURCESERVERNAME%3E%3C%2FSOURCEPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Total%20Time%3A%204m19sec%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1743714414%22%20id%3D%22toc-hId--1743714414%22%3ETest%20data%20move%20with%20%5CCOPY%20(no%20parallelism)%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20use%20%5CCOPY%20to%20move%20data.%20Here%20are%20the%20steps%20we%E2%80%99ll%20follow%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EPreparation%3A%20use%20pg_dump%20and%20psql%20to%20move%20schema%3C%2FLI%3E%0A%3CLI%3EGenerate%20%5CCOPY%20command%20pipes%3C%2FLI%3E%0A%3CLI%3ECopy%20data%20through%20%5CCOPY%20command%20pipes%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Etime%20PGPASSWORD%3D%3CSOURCEPASSWORD%3E%20pg_dump%20-C%20-Fc%20--schema-only%20-h%20%3CSOURCESERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CSOURCESERVERNAME%3E%20tests%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20pg_restore%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20--schema-only%20-C%20-c%20-d%20postgres%0A%0Areal%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m15.288s%0Auser%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m0.144s%0Asys%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m0.054s%0A%0Atime%20for%20j%20in%20%60echo%20%22SELECT%20table_name%20FROM%20information_schema.tables%20WHERE%20table_schema%20%3D%20'public'%20and%20table_type%3D'BASE%20TABLE'%20ORDER%20BY%20table_name%22%7C.%2Fpgsource.sh%60%0Ado%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20echo%20%22%5Ccopy%20%24j%20FROM%20PROGRAM%20'echo%20copy%20binary%20%24j%20to%20STDOUT%7C.%2Fpgsource.sh'%20WITH%20BINARY%3B%22%7C.%2Fpgdestination.sh%0Adone%0A%0Areal%26nbsp%3B%26nbsp%3B%26nbsp%3B%207m47.179s%0Auser%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m19.518s%0Asys%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%200m23.467s%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FSOURCESERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FSOURCESERVERNAME%3E%3C%2FSOURCEPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Total%20Time%3A%208m2sec%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-743798419%22%20id%3D%22toc-hId-743798419%22%3ETest%20alternative%20restoration%20with%20%5CCOPY%20(parallelism)%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUnlike%20parallelized%20pg_dump%20%2F%20pg_restore%2C%20there's%20no%20need%20to%20move%20data%20to%20a%20staging%20file.%20We%20can%20continue%20to%20use%20the%20pipe%20mechanism%20from%20%5CCOPY%20without%20parallelism.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eexport%20maxParal%3D4%0A%26gt%3BcurParal.tmp%0Atime%20for%20j%20in%20%60echo%20%22SELECT%20table_name%20FROM%20information_schema.tables%20WHERE%20table_schema%20%3D%20'public'%20and%20table_type%3D'BASE%20TABLE'%20ORDER%20BY%20table_name%22%7C.%2Fpgsource.sh%60%0Ado%0A%26nbsp%3B%20echo%20%24j%20%26gt%3B%26gt%3BcurParal.tmp%0A%26nbsp%3B%20nohup%20%24SHELL%20%26lt%3B%3CEOF%3E%3C%2FEOF%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20Total%20Time%3A%203m14sec%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-733295315%22%20id%3D%22toc-hId-733295315%22%3E%5CCOPY's%20advantage%20over%20pg_dump%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWithout%20parallelism%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20pg_dump%2Fpg_restore%20won%20with%206m15sec%20vs%208m2sec%20with%20%5CCOPY%20approach%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20parallelism%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%5CCOPY%20approach%20won%20with%203m14sec%20vs%204m19sec%20of%20pg_dump%2Fpg_restore%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlthough%20pg_dump%2Fpg_restore%20uses%20the%20COPY%20command%20internally%2C%20when%20we%20use%20a%20manual%20parallelism%20mechanism%20we%20can%20obtain%20better%20execution%20times.%20The%20performance%20improvement%20comes%20because%20with%20parallel%20%5CCOPY%20we%20can%20use%20pipes%20and%20avoid%20saving%20data%20to%20a%20staging%20area%2C%20as%20pg_dump%2Fpg_restore%20requires%20when%20using%20parallelism.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1074159148%22%20id%3D%22toc-hId--1074159148%22%3EUsing%20%5CCOPY%20to%20ingest%20from%20http%20datasources%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUsing%20%5CCOPY%20command%20we%20can%20ingest%20not%20only%20.csv%2C%20txt%20or%20binary%20files%2C%20%26nbsp%3Bdata%20or%20copy%20from%20another%20database%20through%20pipes%2C%20we%20can%20create%20authentic%20ETL-like%20processes%20with%20a%20single%20command.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20case%20we%20are%20going%20to%20ingest%20the%20open%20csv%20dataset%20%3CA%20href%3D%22https%3A%2F%2Fdata.europa.eu%2Feuodp%2Fdata%2Fdataset%2Fcovid-19-coronavirus-data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ECOVID-19%20cases%20worldwide%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%E2%80%99ll%20create%20a%20Postgres%20table%20called%20covid19casesww%20with%20the%20columns%20we%20need%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eecho%20%22create%20table%20covid19casesww%20(dateRep%20date%2Cday%20integer%2Cmonth%20integer%2Cyear%20integer%2Ccases%20integer%2Cdeaths%20integer%2CcountriesAndTerritories%20text%2CgeoId%20text%2CcountryterritoryCode%20text%2CpopData2019%20integer%2CcontinentExp%20text)%3B%22%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-t%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20ingest%20the%20data%20into%20our%20Postgres%20server%20with%20a%20pipe%20from%20wget%20to%20the%20%5CCOPY%20command%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eecho%20%22set%20datestyle%20to%20SQL%2CDMY%3B%5CCOPY%20covid19casesww%20FROM%20PROGRAM%20'wget%20-q%20-O%20-%20%22%24%40%22%20%22https%3A%2F%2Fopendata.ecdc.europa.eu%2Fcovid19%2Fcasedistribution%2Fcsv%22%7Ctail%20-n%20%2B2'%20CSV%20DELIMITER%20'%2C'%3B%22%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-t%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20we%20can%20access%20the%20data%20and%20calculate%2C%20for%20example%2C%20the%2010%20territories%20with%20the%20lowest%20number%20of%20cases%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eecho%20%22select%20sum(cases)%20as%20cases%2C%20countriesAndTerritories%2C%20continentexp%20from%20covid19casesww%20group%20by%20countriesAndTerritories%2CcontinentExp%20order%20by%201%20asc%20limit%2010%3B%22%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%0A%0Acases%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20countriesandterritories%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20continentexp%0A-------%2B-----------------------------------%2B--------------%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%203%20%7C%20Anguilla%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%207%20%7C%20Bonaire%2C%20Saint%20Eustatius%20and%20Saba%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%208%20%7C%20British_Virgin_Islands%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2011%20%7C%20Montserrat%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2011%20%7C%20Papua_New_Guinea%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20Oceania%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2011%20%7C%20Seychelles%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20Africa%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2012%20%7C%20Holy_See%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20Europe%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2013%20%7C%20Falkland_Islands_(Malvinas)%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2013%20%7C%20Greenland%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%2015%20%7C%20Saint_Kitts_and_Nevis%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%20America%0A(10%20rows)%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1413353685%22%20id%3D%22toc-hId-1413353685%22%3EUsing%20%5CCOPY%20to%20ingest%20from%20other%20database%20engines%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESuppose%20we%20want%20migrate%20data%20from%20a%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fmysql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMySQL%20server%3C%2FA%3E%20to%20a%20PostgreSQL%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20create%20the%20destination%20table%20in%20Postgres.%20Mine%20is%20called%20prumysql.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eecho%20%22create%20table%20prumysql%20(a%20text%2C%20b%20integer)%3B%22%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-t%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPipe%20from%20mysql%20to%20%5CCOPY%20command%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Eecho%20%22%5CCOPY%20prumysql%20FROM%20PROGRAM%20'echo%20select%20a%2Cb%20from%20prumysql%7Cmysql%20-h%20%3CSOURCESERVERNAME%3E.mysql.database.azure.com%20-u%20%3CUSERNAME%3E%40%3CSOURCESERVERNAME%3E%20-p%3CSOURCEPASSWORD%3E%20-sN%20--ssl%20tests%20-B'%20%3B%22%7CPGPASSWORD%3D%3CDESTINATIONPASSWORD%3E%20psql%20-t%20-h%20%3CDESTINATIONSERVERNAME%3E.postgres.database.azure.com%20-U%20%3CUSERNAME%3E%40%3CDESTINATIONSERVERNAME%3E%20tests%3C%2FDESTINATIONSERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FDESTINATIONSERVERNAME%3E%3C%2FDESTINATIONPASSWORD%3E%3C%2FSOURCEPASSWORD%3E%3C%2FSOURCESERVERNAME%3E%3C%2FUSERNAME%3E%3C%2FSOURCESERVERNAME%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--646365117%22%20id%3D%22toc-hId--646365117%22%3E%2FCOPY%20as%20an%20alternative%20way%20to%20do%20PostgreSQL%20data%20migration%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgreSQL%E2%80%99s%20%5CCOPY%20enables%20a%20range%20of%20data%20loading%20experiences.%20We've%20shared%20some%20examples%20in%20this%20blog.%20How%20do%20you%20use%20%5CCOPY%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1561266%22%20slang%3D%22en-US%22%3E%3CP%3EExamples%20of%20how%20to%20load%20data%20with%20Postgres%20COPY%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Copy-files-image-teaser.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F218238i343C2144B573D3D8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Copy-files-image-teaser.png%22%20alt%3D%22Copy-files-image-teaser.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1561266%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Ecopy%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eetl%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMigration%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMySQL%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epg_dump%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epipe%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgresql%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostres%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

When you want to move data into your PostgreSQL database, there are a few options available like pg_dump and Azure Data Factory. The method you pick depends on the scenario you want to enable. Today, we’re exploring three scenarios where you can consider using PostgreSQL’s COPY command.

 

COPY is the Postgres method of data-loading. Postgres's COPY comes in two separate variants, COPY and \COPY: COPY is server based, \COPY is client based.” - The PostgreSQL Wiki

 

Both versions of COPY move data from a file to a Postgres table.

 

The server based COPY command has limited file access and user permissions, and isn’t available for use on Azure Database for PostgreSQL.

 

\COPY runs COPY internally, but with expanded permissions and file access. The source file does not have to exist on the same machine as the Postgres instance if you use \COPY. You can even use both absolute and relative paths.

 

\COPY is a psql feature. You need to have the psql application to be able to use \COPY.

 

Today, we’ll use \COPY to enable these three scenarios:

 

  • Migrating data from Postgres to Postgres
  • Ingesting from http data sources
  • Moving data from other database engines, like MySQL

 

Migrating data from Postgres to Postgres

 

For our first scenario, we’ll do a data migration between two PostgreSQL servers. We’ll first move the data with pg_dump, with and without parallelism. Then we’ll compare the performance using \COPY.

 

These are the advantages and disadvantages I expect to see:

 

Advantages

  • \COPY command goes through pipe, so no space required on client side
  • Potential for increased performance with parallelism, compared to pg_dump

Disadvantages

  • \COPY method does not migrate indexes, foreign keys, and other similar objects. You’ll have to take additional steps to move those objects.  Alternatively, you can consider using a migration service.

 

For this example, I will be migrating from an Azure Database for PostgreSQL Basic tier single Server to a General Purpose tier single server.

 

Prepare psql connection string scripts

 

First, I’ll store the psql connection strings to my source and destination servers in scripts. That way I can call those scripts when I want to connect.

 

pgsource.sh = bash script with psql and connection string to source `tests` database

pgdestination.sh = bash script with psql and connection string to destination `tests` database

 

cat <<EOF > pgsource.sh
PGPASSWORD=<sourcePassword> psql -t -h <sourceServerName>.postgres.database.azure.com -U <username>@<sourceServerName> tests
EOF
chmod +x pgsource.sh

cat <<EOF > pgdestination.sh
PGPASSWORD=<destinationPassword> psql -t -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests
EOF
chmod +x pgdestination.sh

 

 

 

My source database has 20 tables each with 1 million auto-generated rows. There are no indexes or foreign keys in this database.

 

Test full restoration with pg_dump/pg_restore (no parallelism)

 

Internally, pg_dump and pg_restore uses the COPY command

 

time PGPASSWORD=<sourcePassword> pg_dump -C -Fc -h <sourceServerName>.postgres.database.azure.com -U <username>@<sourceServerName> tests|PGPASSWORD=<destinationPassword> pg_restore -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> -C -c -d postgres

real    6m15.310s
user    0m22.976s
sys     0m9.393s

 

 

 

- Total Time: 6m15sec

 

Test full restoration with pg_dump/pg_restore (parallel)

 

We’ll need to use the directory format export, and dump to regular files. (See the pg_dump, pg_restore documentation to learn about format options).

 

time PGPASSWORD=<sourcePassword> pg_dump -C -Fd -h <sourceServerName>.postgres.database.azure.com -U <username>@<sourceServerName> tests -j 4 -f testsdir

real    3m23.004s
user    0m20.291s
sys     0m13.888s

time PGPASSWORD=<destinationPassword> pg_restore -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> -C -c -d postgres -j 4 testsdir

real    0m56.175s
user    0m1.671s
sys     0m0.478s

 

 

- Total Time: 4m19sec

 

 

Test data move with \COPY (no parallelism)

 

Let's use \COPY to move data. Here are the steps we’ll follow:

  • Preparation: use pg_dump and psql to move schema
  • Generate \COPY command pipes
  • Copy data through \COPY command pipes

 

time PGPASSWORD=<sourcePassword> pg_dump -C -Fc --schema-only -h <sourceServerName>.postgres.database.azure.com -U <username>@<sourceServerName> tests|PGPASSWORD=<destinationPassword> pg_restore -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> --schema-only -C -c -d postgres

real    0m15.288s
user    0m0.144s
sys     0m0.054s

time for j in `echo "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_type='BASE TABLE' ORDER BY table_name"|./pgsource.sh`
do
       echo "\copy $j FROM PROGRAM 'echo copy binary $j to STDOUT|./pgsource.sh' WITH BINARY;"|./pgdestination.sh
done

real    7m47.179s
user    0m19.518s
sys     0m23.467s

 

 

- Total Time: 8m2sec

 

Test alternative restoration with \COPY (parallelism)

 

Unlike parallelized pg_dump / pg_restore, there's no need to move data to a staging file. We can continue to use the pipe mechanism from \COPY without parallelism.

 

export maxParal=4
>curParal.tmp
time for j in `echo "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_type='BASE TABLE' ORDER BY table_name"|./pgsource.sh`
do
  echo $j >>curParal.tmp
  nohup $SHELL <<EOF &
  echo "\copy $j FROM PROGRAM 'echo copy binary $j to STDOUT|./pgsource.sh' WITH BINARY;"|./pgdestination.sh; sed -i '/^$j$/d' curParal.tmp
EOF
  while [ `wc -l curParal.tmp|cut -f 1 -d " "` -ge $maxParal ]
  do
    sleep 1 
  done 
  echo $curParal $maxParal
done

real    2m59.491s
user    0m24.388s
sys     0m40.200s

 

 

- Total Time: 3m14sec

 

\COPY's advantage over pg_dump

 

Without parallelism:

                pg_dump/pg_restore won with 6m15sec vs 8m2sec with \COPY approach

 

With parallelism:

                \COPY approach won with 3m14sec vs 4m19sec of pg_dump/pg_restore

 

Although pg_dump/pg_restore uses the COPY command internally, when we use a manual parallelism mechanism we can obtain better execution times. The performance improvement comes because with parallel \COPY we can use pipes and avoid saving data to a staging area, as pg_dump/pg_restore requires when using parallelism.

       

Using \COPY to ingest from http datasources

 

Using \COPY command we can ingest not only .csv, txt or binary files,  data or copy from another database through pipes, we can create authentic ETL-like processes with a single command.

  

In this case we are going to ingest the open csv dataset COVID-19 cases worldwide.

 

First, we’ll create a Postgres table called covid19casesww with the columns we need:

 

echo "create table covid19casesww (dateRep date,day integer,month integer,year integer,cases integer,deaths integer,countriesAndTerritories text,geoId text,countryterritoryCode text,popData2019 integer,continentExp text);"|PGPASSWORD=<destinationPassword> psql -t -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests

 

 

We ingest the data into our Postgres server with a pipe from wget to the \COPY command

 

echo "set datestyle to SQL,DMY;\COPY covid19casesww FROM PROGRAM 'wget -q -O - "$@" "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv"|tail -n +2' CSV DELIMITER ',';"|PGPASSWORD=<destinationPassword> psql -t -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests

 

 

 

Now we can access the data and calculate, for example, the 10 territories with the lowest number of cases:

 

echo "select sum(cases) as cases, countriesAndTerritories, continentexp from covid19casesww group by countriesAndTerritories,continentExp order by 1 asc limit 10;"|PGPASSWORD=<destinationPassword> psql -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests

cases |      countriesandterritories      | continentexp
-------+-----------------------------------+--------------
     3 | Anguilla                          | America
     7 | Bonaire, Saint Eustatius and Saba | America
     8 | British_Virgin_Islands            | America
    11 | Montserrat                        | America
    11 | Papua_New_Guinea                  | Oceania
    11 | Seychelles                        | Africa
    12 | Holy_See                          | Europe
    13 | Falkland_Islands_(Malvinas)       | America
    13 | Greenland                         | America
    15 | Saint_Kitts_and_Nevis             | America
(10 rows)

 

 

 

Using \COPY to ingest from other database engines

 

Suppose we want migrate data from a MySQL server to a PostgreSQL server.

 

First create the destination table in Postgres. Mine is called prumysql.

 

echo "create table prumysql (a text, b integer);"|PGPASSWORD=<destinationPassword> psql -t -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests

 

 

 

Pipe from mysql to \COPY command

 

echo "\COPY prumysql FROM PROGRAM 'echo select a,b from prumysql|mysql -h <sourceServerName>.mysql.database.azure.com -u <username>@<sourceServerName> -p<sourcePassword> -sN --ssl tests -B' ;"|PGPASSWORD=<destinationPassword> psql -t -h <destinationServerName>.postgres.database.azure.com -U <username>@<destinationServerName> tests

 

 

 

/COPY as an alternative way to do PostgreSQL data migration

 

PostgreSQL’s \COPY enables a range of data loading experiences. We've shared some examples in this blog. How do you use \COPY?