<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bilal</title>
    <description>The latest articles on DEV Community by Bilal (@glowinginthedark).</description>
    <link>https://dev.to/glowinginthedark</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F260093%2F4e65ef38-1d22-4dd1-b3cb-f7f20177ff39.jpeg</url>
      <title>DEV Community: Bilal</title>
      <link>https://dev.to/glowinginthedark</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/glowinginthedark"/>
    <language>en</language>
    <item>
      <title>What is a REST API?</title>
      <dc:creator>Bilal</dc:creator>
      <pubDate>Sun, 10 Mar 2024 19:53:13 +0000</pubDate>
      <link>https://dev.to/glowinginthedark/what-is-a-rest-api-4ng6</link>
      <guid>https://dev.to/glowinginthedark/what-is-a-rest-api-4ng6</guid>
      <description>&lt;p&gt;As a software engineer that works regularly on back-end systems, a &lt;strong&gt;REST API&lt;/strong&gt; is one of the most important things that I work with on a daily basis. In this post I'm going to explain in detail, and &lt;em&gt;in my own style&lt;/em&gt; what a REST API is, its significance in web applications and business operations, as well as how to build one super easily with JavaScript.&lt;/p&gt;

&lt;p&gt;Note: The acronym REST API = &lt;strong&gt;RE&lt;/strong&gt;presentational &lt;strong&gt;S&lt;/strong&gt;tate Transfer &lt;strong&gt;A&lt;/strong&gt;pplication &lt;strong&gt;P&lt;/strong&gt;rogrammable &lt;strong&gt;I&lt;/strong&gt;nterface&lt;/p&gt;

&lt;h2&gt;
  
  
  Interfaces
&lt;/h2&gt;

&lt;p&gt;First, I want to get into what an interface is. I will provide 2 real life analogies here so you have reference points to think with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Traffic Lights 🚦
&lt;/h3&gt;

&lt;p&gt;A traffic light is like a contract between all people that use the roads:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your side has a red light, everyone on your side must stop&lt;/li&gt;
&lt;li&gt;Similarly, if your side shows a green light, everyone on your side must move and go&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can think of this "system" and the "definition" of traffic lights (whatever we understand it as) as an &lt;strong&gt;interface&lt;/strong&gt; for communication between humans that use roads, whether to drive on them, ride a bicycle or walk as a pedestrian.&lt;/p&gt;

&lt;p&gt;What I want you to imagine is something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uar5tz31rn2jyhbf1td.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uar5tz31rn2jyhbf1td.png" alt="A simple graphic showing what an interface is like" width="588" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following this graphic, a traffic light is an interface that all parties (drivers on the road) are using to access information about whether to stop at a junction or keep moving. The traffic light distributes this information to anyone who wants to "access" it (or look at it). In this case, the parties are not sending any information "to" the traffic light; they are just receiving.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Human language
&lt;/h3&gt;

&lt;p&gt;If you speak Spanish with your family, say with your brother, then the rules and grammar of the language as well as the words all make up the definition of the &lt;strong&gt;interface&lt;/strong&gt; that you both use to communicate between you. In addition, facial expressions - like raised eyebrows - or using hands are also a part of a commonly understood human communication interface.&lt;/p&gt;

&lt;p&gt;There are many more fascinating examples that we can find in our world. The United Nations is an interface for communication between nation states of the world, and the Premier League table is an interface that we use to know which football teams are the very best in England.&lt;/p&gt;

&lt;p&gt;You'll notice that interfaces can be very open ended and can define rules at any level. The networking protocols that make up the internet are extremely powerful interfaces because we all use them in our daily lives. On the other hand, a schedule sheet that keeps track of shifts at a local coffee shop is an interface that is only relevant to the folks that work at that coffee shop.&lt;/p&gt;




&lt;h2&gt;
  
  
  APIs
&lt;/h2&gt;

&lt;p&gt;An API is basically an interface for computer programs. Your phone knows how to display this article to you in a form that you can read because of many different interfaces (IP - Internet Protocol, HTML, CSS, JavaScript and many more). Some of these interfaces define a language like JavaScript, and others define what structure information should be sent in between computers so they all know how to read it and what it means.&lt;/p&gt;

&lt;p&gt;A REST API defines &lt;strong&gt;custom&lt;/strong&gt; rules for how applications talk to each other on the web. This is what makes REST APIs so powerful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Managing business with customers using REST APIs
&lt;/h3&gt;

&lt;p&gt;Let's say that you own a pet supplies store and you want to delight your customers and clients by letting them sign up on a web app and register for monthly subscriptions for supplies at a discounted price - supplies like dog food or pet cleaning supplies.&lt;/p&gt;

&lt;p&gt;Your app would store this information in a database like PostgreSQL or MySQL, and you would define a custom set of rules (i.e. an interface) for letting customers access this information.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your customers would likely want to send over their payment method information, like credit card details&lt;/li&gt;
&lt;li&gt;They'd want to see what products are available to buy, and whether or not they can be bought once or continuously via a monthly subscription&lt;/li&gt;
&lt;li&gt;In addition, you'd want to be able to add and remove products, define their prices, and maybe manage access for users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a proper real-life business example, you'd actually be setting up numerous different APIs (interfaces) for managing this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You'd have an API for managing users - doing operations like creating a user, editing a user, deleting a user&lt;/li&gt;
&lt;li&gt;You'd have an API for managing products&lt;/li&gt;
&lt;li&gt;An API for managing subscriptions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and so on...&lt;/p&gt;

&lt;p&gt;You as the owner of the store would have more capabilities to do more with information than your customers, but each party would have ways to send and receive information using the interface or API.&lt;/p&gt;




&lt;h3&gt;
  
  
  Example walkthrough
&lt;/h3&gt;

&lt;p&gt;If you wanted to see how many of your customers are subscribed to receive dog food packages via delivery on a monthly basis, this is how it would work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your front-end application (on your phone or desktop computer) would send a piece of information like &lt;code&gt;/subscriptions?type=monthly&amp;amp;category=dogfood&lt;/code&gt; to your back-end application. Both of these applications would have knowledge about the structure and definition of your API(s)&lt;/li&gt;
&lt;li&gt;Your back-end application (running on a server somewhere) would receive this piece of information and break it down, &lt;em&gt;&lt;strong&gt;following the rules and definitions of your API (interface)&lt;/strong&gt;&lt;/em&gt; defined in the code of your application - this could be in any language, like JavaScript, C#, Ruby or Python&lt;/li&gt;
&lt;li&gt;Your web app would check whether you are who you say you are (authentication) and whether you (the person requesting for this information) actually have the rights to access this information (authorization)&lt;/li&gt;
&lt;li&gt;Next, your web app will retrieve the information from your storage (database) and send it back &lt;strong&gt;in a form that is strictly defined by your API&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Lastly, when you see this information on your phone or your desktop computer, your browser will know how to display it to you because your web app defines the structure of this information on both the back-end side as well as the front-end side&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Building a simple REST API with JavaScript and express.js
&lt;/h2&gt;

&lt;p&gt;Express.js is a library in the JavaScript ecosystem that empowers programmers like you and I to easily spin up REST APIs. Let's build one right now.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;You want to make sure that you have &lt;a href="https://nodejs.org/en"&gt;Node.js&lt;/a&gt; and &lt;a href="https://www.npmjs.com/"&gt;NPM&lt;/a&gt; installed on your system (just google how to do it if you don't know - it's very easy). Next, create a new folder/directory anywhere and open a terminal/command prompt window inside it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Then, Initialize your project using the command &lt;code&gt;npm init -y&lt;/code&gt; and install express.js using &lt;code&gt;npm install express&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;Create a new file called &lt;code&gt;index.js&lt;/code&gt; in your folder/directory and paste the following code below (and obviously, save it):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Did you work out today?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a very simple version of a back-end application which has only one API named &lt;code&gt;/&lt;/code&gt;. If we send a &lt;code&gt;GET&lt;/code&gt; request for information to this API, we will get the information &lt;code&gt;Did you workout today?&lt;/code&gt; back.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET&lt;/code&gt; is a &lt;strong&gt;type&lt;/strong&gt; of information request that we can define in a REST API when we are creating it. There are many other clear and well defined types of information requests in the REST ecosystem including, you can learn more about this &lt;a href="https://restfulapi.net/http-methods/"&gt;here&lt;/a&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;POST&lt;/code&gt; - With this type of request, we can include chunks of information in the &lt;em&gt;request body&lt;/em&gt; as an object, which we then send to the back-end server every time we trigger this request&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT&lt;/code&gt; - Tells the back-end server that we want to update something, like updating the price of a product or changing the email of a user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE&lt;/code&gt; - This type of request indicates to the back-end server that we want to delete something&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 4
&lt;/h3&gt;

&lt;p&gt;Back in your terminal/command prompt, start your back-end server application by running the command &lt;code&gt;node index.js&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5
&lt;/h3&gt;

&lt;p&gt;Open a new terminal window and execute the command &lt;code&gt;curl http://localhost:3000/&lt;/code&gt;. This simulates sending a request to your back-end API. The &lt;code&gt;/&lt;/code&gt; at the end tells the application what information we want to request.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;PS: &lt;code&gt;curl&lt;/code&gt; is a simple terminal/command line tool for sending HTTP requests to any server, whether on your computer or anywhere on the internet, and &lt;code&gt;localhost&lt;/code&gt; is an address alias that represents your own computer. You should try executing &lt;code&gt;curl https://www.google.com&lt;/code&gt; to see that we can also get webpages from the internet using this tool.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Your terminal should output this text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Did you work out today?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have just defined a very simple API.&lt;/p&gt;

&lt;p&gt;Now, let's extend this and implement a very simple version of the pet store &lt;code&gt;products&lt;/code&gt; API where a customer can ask us "what products do you have?" and we respond with a list of products.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6
&lt;/h3&gt;

&lt;p&gt;Add the following lines of code to your &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;const express = require('express');
const app = express();
const port = 3000;
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;app.get('/', (req, res) =&amp;gt; res.send('Did you work out today?'));
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+ const products = [
+  { id: 1, name: 'Dog Food', price: 10.99 },
+  { id: 2, name: 'Boat Plush Toy', price: 15.99 },
+  { id: 3, name: 'Cat Scratching Post', price: 19.99 }
+ ];
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+ app.get('/products', (req, res) =&amp;gt; {
+  res.json(products);
+ });
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;app.listen(port, () =&amp;gt; console.log(`Server running on port ${port}`));
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are adding a list (array) of products and we add a new definition for an API &lt;strong&gt;route&lt;/strong&gt; called &lt;code&gt;/products&lt;/code&gt; which returns the list of products. Thus, our users can call the API and request for this information like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl http://localhost:3000/products
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila! 🎉&lt;/p&gt;




&lt;p&gt;This is how easy it is to create simple REST APIs using JavaScript, and I would highly recommend this process for testing your personal projects.&lt;/p&gt;

&lt;p&gt;It should be noted that actual business applications and complex back-end systems have a lot more detail, configuration and definitions than this. If you'd like to read more about more complex systems and enterprise level programming that supports high-level business, let me know in the comments and follow me to get updates for future posts.&lt;/p&gt;

&lt;p&gt;If you have any questions or want to ask me more about anything related, let me know as well in the comments below.&lt;/p&gt;

&lt;p&gt;You can also follow me on X &lt;a href="https://twitter.com/mbzdotdev"&gt;@mbzdotdev&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cheers.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Here's how you can upload tons of data to a PostgreSQL database in a snap</title>
      <dc:creator>Bilal</dc:creator>
      <pubDate>Fri, 07 Jan 2022 12:18:24 +0000</pubDate>
      <link>https://dev.to/glowinginthedark/heres-how-you-can-upload-tons-of-data-to-a-postgresql-database-in-a-snap-3n94</link>
      <guid>https://dev.to/glowinginthedark/heres-how-you-can-upload-tons-of-data-to-a-postgresql-database-in-a-snap-3n94</guid>
      <description>&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;A CSV file containing data&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  1. Install psycopg2
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/psycopg2/"&gt;psycopg2&lt;/a&gt; is the python library we are going to use. Run the following command in your terminal to install the package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install psycopg2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Imports
&lt;/h3&gt;

&lt;p&gt;Spin up a text editor and add these imports to your python file. We will use the csv package to import data from a csv file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2.sql&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;psql&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2.extras&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're curious, &lt;code&gt;psycopg2.sql&lt;/code&gt; contains SQL related helpers for generating SQL queries using python, and &lt;code&gt;psycopg2.extras&lt;/code&gt; contains the most important method of this tutorial - &lt;a href="https://www.psycopg.org/docs/extras.html#psycopg2.extras.execute_values"&gt;execute_values()&lt;/a&gt; - which we will use to finally execute the import query.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Create a database connection object and cursor
&lt;/h3&gt;

&lt;p&gt;These objects can be treated as a &lt;a href="https://en.wikipedia.org/wiki/Singleton_pattern"&gt;singletons&lt;/a&gt; which will hold the connection to the database and they can be used multiple times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;dsn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;dbname&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your_database&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;postgres&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;db_cursor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Load the data from your csv file
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Place your csv file in the same directory as your python script (or just update the file path to point to wherever it lives) &lt;/li&gt;
&lt;li&gt;You can use the following code to easily import the csv data
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your_csv_file.csv&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delimiter&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;allrows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;allrows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;column1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;column2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;line&lt;/code&gt; in the for loop is a list of all the values in the csv row currently being looped through. &lt;code&gt;line[0]&lt;/code&gt; would be the first field, &lt;code&gt;line[1]&lt;/code&gt; the second, and so on.&lt;/li&gt;
&lt;li&gt;For each &lt;code&gt;line&lt;/code&gt;/row in the csv that is looped over, we create a dictionary (map) where the keys are the field/column names and the values are... the values. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;allrows&lt;/code&gt; variable will store a list of all the created dictionaries (each representing a row which we will insert).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;PS: It's important that the keys be the exact same as the column names into the table you want to insert your data into.&lt;/strong&gt; This consistency is required to make the rest of the steps below work. You can surely choose to use different names, but then you'd need to also update the steps below  to work with that.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Generate re-usable SQL
&lt;/h3&gt;

&lt;p&gt;In this step, we will generate some SQL using the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column_names&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INSERT INTO {} ({}) VALUES %s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Identifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Identifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column_names&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;query2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;({})&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;psql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Placeholder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column_names&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;query2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The helper functions used here make it easy to dynamically create SQL queries based on how many, and which columns we want to update&lt;/li&gt;
&lt;li&gt;We are not really running 2 queries, but &lt;code&gt;query2&lt;/code&gt; represents a portion of the dynamically generated SQL - specifically the part that comes after &lt;code&gt;VALUES&lt;/code&gt; in a typical insert SQL query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Call the function with the name of the table you want to update, as well as the column names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;insert_sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;template_sql&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;create_sql&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;your_table&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;column1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;column2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I encourage you to add print statements or breakpoints to inspect what different values look like. Below, you can see that I've printed &lt;code&gt;allrows&lt;/code&gt;, &lt;code&gt;insert_sql&lt;/code&gt; and &lt;code&gt;template_sql&lt;/code&gt; to see how my dummy data will get inserted: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fayg699sops2u6h8oa34p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fayg699sops2u6h8oa34p.png" alt="Print output showing the generated SQL" width="387" height="53"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Execute the query
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;psycopg2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute_values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;db_cusror&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;insert_sql&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allrows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;template_sql&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the changes are not yet committed. This is a good checkpoint to test for errors (i.e. if you get no errors until this point, it means you're good to perform the final step).&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Commit the changes 🎉
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;commit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Congratulations! You can now re-use this script to import huge amounts of data with a single command 🪄&lt;/p&gt;

&lt;p&gt;If you have questions about any part or thoughts you want to share, feel free to mention those in the comments below :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>python</category>
      <category>database</category>
    </item>
  </channel>
</rss>
