<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CSV File &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/csv-file/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 15 Sep 2017 11:43:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server: xp_cmdshell and BCP to export table data in a Text File</title>
		<link>https://www.dbrnd.com/2017/03/sql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 23 Mar 2017 16:16:22 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[BCP Command]]></category>
		<category><![CDATA[CSV File]]></category>
		<category><![CDATA[data migration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[Text file]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[XP_CMDSHELL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4803</guid>

					<description><![CDATA[In this post, I am sharing a kind of utility article which you can use to export your records or data from SQL Server Database to Windows Text File. Let me share, where I am using this. I am monitoring the performance of a few critical stored procedures by logging in line important messages. Later, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&amp;linkname=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration%2F&#038;title=SQL%20Server%3A%20xp_cmdshell%20and%20BCP%20to%20export%20table%20data%20in%20a%20Text%20File" data-a2a-url="https://www.dbrnd.com/2017/03/sql-server-xp_cmdshell-and-bcp-to-export-table-data-in-a-text-file-data-migration/" data-a2a-title="SQL Server: xp_cmdshell and BCP to export table data in a Text File"></a></p><p>In this post, I am sharing a kind of utility article which you can use to export your records or data from SQL Server Database to Windows Text File.</p>
<blockquote><p>Let me share, where I am using this. I am monitoring the performance of a few critical stored procedures by logging in line important messages.</p>
<p>Later, I am saving this details into a text file and sending that text file as an email attachment. We can also directly send table data as an email body, but we require into files for other tools also.</p></blockquote>
<p>You can use xp_cmdshell and BCP command to export your data from SQL Server to Text File.<br />
If you are getting an error like below, enable xp_cmdshell. You can use below reference article to enable xp_cmdshell.</p><pre class="crayon-plain-tag">Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. 
A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', search for 'xp_cmdshell' in SQL Server Books Online.</pre><p>
<blockquote class="wp-embedded-content" data-secret="bs4P15yS1H"><p><a href="https://www.dbrnd.com/2015/11/sql-server-script-to-enable-and-disable-xp_cmdshell-configuration/">SQL Server: Script to Enable and Disable XP_CMDSHELL Configuration</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/11/sql-server-script-to-enable-and-disable-xp_cmdshell-configuration/embed/#?secret=bs4P15yS1H" data-secret="bs4P15yS1H" width="600" height="338" title="&#8220;SQL Server: Script to Enable and Disable XP_CMDSHELL Configuration&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Create a table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_Students
(
	Rno INT PRIMARY KEY
	,STudName VARCHAR(20)
	,ClassName CHAR(1)
)
GO

INSERT INTO tbl_Students 
VALUES 
(1,'Anvesh','A'),(2,'Neevan','B'),(3,'Toby','C')
,(5,'Roy','A'),(4,'Jenny','B'),(6,'Kaviy','C')
,(7,'Martin','A'),(8,'Laxmi','B'),(9,'Nion','C')
GO</pre><p>
<strong>Execute xp_cmdshell to export data in text file:</strong></p><pre class="crayon-plain-tag">EXEC xp_cmdshell 'bcp "SELECT * FROM dbo.tbl_Students" queryout "C:\Test\StudentData.txt" -T -c -t,'</pre><p>
<strong><img fetchpriority="high" decoding="async" class="alignnone wp-image-4807 size-full" title="SQL Server Export Data In Text File" src="https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataInTextFile.png" alt="SQL Server Export Data In Text File" width="403" height="165" srcset="https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataInTextFile.png 403w, https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataInTextFile-300x123.png 300w, https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataInTextFile-150x61.png 150w" sizes="(max-width: 403px) 100vw, 403px" /></strong></p>
<p><strong>Check the text file:</strong></p>
<p><img decoding="async" class="alignnone wp-image-4806 size-full" title="SQL Server Export Data Result In Text" src="https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataResultInText.png" alt="SQL Server Export Data Result In Text" width="407" height="240" srcset="https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataResultInText.png 407w, https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataResultInText-300x177.png 300w, https://www.dbrnd.com/wp-content/uploads/2017/03/SQLServerExportDataResultInText-150x88.png 150w" sizes="(max-width: 407px) 100vw, 407px" /></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: Import and Export CSV data with Headers</title>
		<link>https://www.dbrnd.com/2015/09/mysql-import-and-export-csv-data-with-headers/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 29 Sep 2015 17:23:16 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[CSV File]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=744</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on how to export or import CSV data using MySQL INFILE and OUTFILE. The CSV files are most famous for the basic data migration activities. Database developer does not need to write or create any particular database link between two servers. They can easily import data from [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&amp;linkname=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fmysql-import-and-export-csv-data-with-headers%2F&#038;title=MySQL%3A%20Import%20and%20Export%20CSV%20data%20with%20Headers" data-a2a-url="https://www.dbrnd.com/2015/09/mysql-import-and-export-csv-data-with-headers/" data-a2a-title="MySQL: Import and Export CSV data with Headers"></a></p><p>In this post, I am sharing a demonstration on how to export or import CSV data using MySQL INFILE and OUTFILE.</p>
<blockquote><p>The CSV files are most famous for the basic data migration activities.<br />
Database developer does not need to write or create any particular database link between two servers.<br />
They can easily import data from CSV and export that CSV data into another server and also CSV file does not have any data length limitation like XLS file.<br />
User can store millions of records into CSV file.</p></blockquote>
<p>This technique is preferred only for temporary purposes, and if it continues the process, the user has to write some automatic process like database server replication.</p>
<blockquote data-secret="cChVq1yzf8" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/09/import-or-export-a-csv-file-using-postgresql-copy-command/">Import or Export a CSV File using PostgreSQL COPY Command</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/import-or-export-a-csv-file-using-postgresql-copy-command/embed/#?secret=cChVq1yzf8" data-secret="cChVq1yzf8" width="600" height="338" title="&#8220;Import or Export a CSV File using PostgreSQL COPY Command&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>MySQL provides a different way to manage, data import and export in the CSV format.</p>
<p>You can also perform this exercise using MySQL workbench Import/Export wizard.</p>
<p>Very soon, I will prepare and share a video help for this workbench related solution.</p>
<p>Let me demonstrate it:</p>
<p>First, I created a table and import data from CSV file.<br />
I already created a CSV file with sample data. Make sure that, your table column and CSV column would be same.<br />
You can create this CSV file using a comma delimited save as option of Microsoft excel.</p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png"><img loading="lazy" decoding="async" class="alignnone wp-image-683 size-full" title="MySQL CSV" src="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png" alt="MySQL CSV" width="565" height="493" srcset="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png 565w, https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV-300x262.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV-150x131.png 150w" sizes="auto, (max-width: 565px) 100vw, 565px" /></a></p>
<p><strong> Create a sample table:</strong></p><pre class="crayon-plain-tag">CREATE TABLE TestCSV 
(
    Rno INTEGER
    ,Name VARCHAR(50)
);</pre><p>
<p><strong>Now COPY data from the CSV file into this table:</strong></p><pre class="crayon-plain-tag">LOAD DATA INFILE 'E:/dbrnd.com/TestCSV.csv' 
INTO TABLE TestCSV 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;</pre><p>
<p>You should ignore the first row because of CSV headers.</p>
<p><strong>Check your sample table:</strong></p><pre class="crayon-plain-tag">SELECT *from TestCSV</pre><p>
<a href="https://www.dbrnd.com/wp-content/uploads/2015/09/MySQLCSVTableData.png"><img loading="lazy" decoding="async" class="alignnone wp-image-750 size-full" title="MySQL CSV Table Data" src="https://www.dbrnd.com/wp-content/uploads/2015/09/MySQLCSVTableData.png" alt="MySQL CSV Table Data" width="120" height="205" /></a></p>
<p><strong>You can also import particular column from CSV file to MySQL Table:</strong></p><pre class="crayon-plain-tag">LOAD DATA LOCAL INFILE 'E:/dbrnd.com/TestCSV.csv'  INTO TABLE TestCSV
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'  
IGNORE 1 ROWS
(@col1,@col2) set name=@col2;</pre><p>
<p>As you can see in the above script, I used query variable and @col1 stores all Rno data, and this is working as a dummy import for column Rno.<br />
I set values for Name column by assigning @col2, so this query only import second column data, and the first column would be NULL.</p>
<p><strong>Now, script for export MySQL table data into CSV File:</strong></p><pre class="crayon-plain-tag">SELECT 
    IFNULL(Rno,'N/A')
    ,Name
FROM TestCSV
INTO OUTFILE 'E:/dbrnd.com/TestCSV2.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';</pre><p>
<p>After execution of the above query, please check your newly created CSV File.<br />
Note: This query will create a new CSV file at the specified path.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Import or Export a CSV File using PostgreSQL COPY Command</title>
		<link>https://www.dbrnd.com/2015/09/import-or-export-a-csv-file-using-postgresql-copy-command/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 20 Sep 2015 17:45:59 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[COPY FROM]]></category>
		<category><![CDATA[COPY TO]]></category>
		<category><![CDATA[CSV File]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[export]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=673</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on how to export or import CSV data using the PostgreSQL COPY command. The CSV files are most famous for the necessary data migration activities. Database developer does not need to write or create any particular database link between two servers. They can easily import data in [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&amp;linkname=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F09%2Fimport-or-export-a-csv-file-using-postgresql-copy-command%2F&#038;title=Import%20or%20Export%20a%20CSV%20File%20using%20PostgreSQL%20COPY%20Command" data-a2a-url="https://www.dbrnd.com/2015/09/import-or-export-a-csv-file-using-postgresql-copy-command/" data-a2a-title="Import or Export a CSV File using PostgreSQL COPY Command"></a></p><p>In this post, I am sharing a demonstration on how to export or import CSV data using the PostgreSQL COPY command.</p>
<blockquote><p>The CSV files are most famous for the necessary data migration activities. Database developer does not need to write or create any particular database link between two servers.</p>
<p>They can easily import data in CSV and export that CSV into another server and also CSV file does not have any data length limitation like any other XLS file. </p>
<p>User can store millions of records into CSV file.</p></blockquote>
<p>This technique is preferred only for temporary purposes, and if it continues the process, the user has to write some automated process like database server replication.</p>
<p>PostgreSQL provides the COPY command to import or export CSV data into the PostgreSQL table.</p>
<p>Let me demonstrate this:</p>
<p>First, I create one table and import data from CSV file. I have already created a CSV file with sample data. Make sure that, your table column and CSV column would be same.</p>
<p>You can create this CSV file using a comma delimited save as option of Microsoft excel.</p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png"><img loading="lazy" decoding="async" class="alignnone wp-image-683 size-full" title="Postgres CSV" src="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png" alt="Postgres CSV" width="565" height="493" srcset="https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV.png 565w, https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV-300x262.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/09/PostgresCSV-150x131.png 150w" sizes="auto, (max-width: 565px) 100vw, 565px" /></a></p><pre class="crayon-plain-tag">CREATE TABLE tbl_testcsv
(
  rno integer,
  name character varying
);</pre><p>
<p><strong>Now COPY the data from CSV file into the table:</strong></p><pre class="crayon-plain-tag">COPY tbl_testcsv FROM 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;</pre><p>
<p><strong>After executing of COPY command, you can check the result:</strong></p><pre class="crayon-plain-tag">SELECT *FROM tbl_testcsv;</pre><p>
<a href="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVTablelData.png"><img loading="lazy" decoding="async" class="alignnone wp-image-685 size-full" title="Postgres CSV Tablel Data" src="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVTablelData.png" alt="Postgres CSV Tablel Data" width="230" height="226" srcset="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVTablelData.png 230w, https://www.dbrnd.com/wp-content/uploads/2015/09/CSVTablelData-150x147.png 150w" sizes="auto, (max-width: 230px) 100vw, 230px" /></a></p>
<p><strong>Add some more records in Table and then we export the data into CSV file:</strong></p><pre class="crayon-plain-tag">INSERT INTO tbl_testcsv
VALUES 
(9,'YRQ'),(10,'QWE')
,(11,'MNB'),(12,'FGH');</pre><p>
<p><strong>Now, export this data using the COPY command:</strong></p><pre class="crayon-plain-tag">COPY tbl_testcsv TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;
--Query returned successfully: 12 rows affected,</pre><p>
<p><strong>Now, check your CSV file. You can find newly inserted data:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVExcelData.png"><img loading="lazy" decoding="async" class="alignnone wp-image-687 size-full" title="Postgres CSV Excel Data" src="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVExcelData.png" alt="" width="198" height="336" srcset="https://www.dbrnd.com/wp-content/uploads/2015/09/CSVExcelData.png 198w, https://www.dbrnd.com/wp-content/uploads/2015/09/CSVExcelData-177x300.png 177w, https://www.dbrnd.com/wp-content/uploads/2015/09/CSVExcelData-150x255.png 150w" sizes="auto, (max-width: 198px) 100vw, 198px" /></a></p>
<p><strong>You can also define a particular column or write a full select statement in the COPY command:</strong><br />
Below is a sample script.</p><pre class="crayon-plain-tag">COPY tbl_testcsv(rno)
TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;</pre><p>
</p><pre class="crayon-plain-tag">COPY (SELECT rno,name FROM tbl_testcsv WHERE rno=8)
TO 'E:\dbrnd.com\Postwork\testcsv.csv'
WITH DELIMITER ','
CSV HEADER;</pre><p>
<p>This is a full detailed example of Postgres COPY command, please test this sample and let me know for further assistance.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
