Querying Shopify Data in Postgres

Tags: postgres
Published: 2021-11-15 19:56:58

## Introduction to Shopify

Shopify is a SaaS company that simplifies the process of setting up and running an online business. Businesses that use Shopify have access to important information about their products, orders, customers, and more within Shopify. In most cases, businesses will want to extract this data and combine it with other data into a central database. In this article, we will be discussing how to migrate data from Shopify to Postgres.
​
## Create the Postgres Database

You need to have a Postgres database in which you will store your Shopify data. You can create the database directly from Postgres, in which you will use the `CREATE DATABASE database_name` command. For example to give the database the name *shopify*, run the following command:

```
CREATE DATABASE shopify;
```

The command will create the database within seconds.

You can also use [Heroku’s](https://www.heroku.com/postgres) database-as-a-service platform to create a Postgres database instance. First, create a Heroku account and you will be presented with the “Create New App” screen. Follow the on-screen instructions to create the app.
![](https://lh3.googleusercontent.com/4ArjKrKSEOhKdHI8ARAhKl9AfxrvbsV9Ke7bmkqr3dCtqnykVxWIEPbFoMe15yXQp4XeqXUUwGm09TfIMxffzOfAuSiOptf-seWaG0p-VD3i2jez3lpOlBDfZIG7c91V-git4z4c)

Once the app has been created, click “Resources”. Search for “Postgres”


With Heroku’s free plan, you can’t fully extract enough rows, so choose “Hobby Basic ($9/month)”.
![](https://lh3.googleusercontent.com/XLo0woP8eY8Ks1rJvgHhZtof_DC1BHlhCSOe7i-OEdmdSmT-dwnwA-03zIUshB0CS_HOQbGpf5go9cV5FK7xTPPNE5WpyXzVqa0u49PyfbeCM8e6n1mZMp0cI36ldun8Jqq4PBMx)
Open your dashboard by clicking Heroku Postgres in your addons list. Click the “Settings” tab and then choose “View Credentials”. You will be asked for those credentials later.
​
## Migrate your Shopify Data to Postgres

Our goal is to migrate data about Shopify orders to the Postgres database. Now that our Postgres database is ready, we will use an [ETL (Extract, Transform, Load) service](https://www.stitchdata.com/vs/) to pull Shopify orders data into the database. The three ETL functions enable us to extract data from one database and load it into another.
​
The good thing is that there are many ETL tools that you can use. Examples include Stitchdata and [Fivetran](https://fivetran.com/connectors/shopify). The latter will be good for you as you scale, thus, we will use Stitchdata.
​
Sign up for a Stitchdata account. Click the “Destination” tab from the top and choose “Postgres”. Enter the credentials of your Postgres database. For screenshots of this part of the process, refer to our article on hooking up [Stripe to Postgres](__GHOST_URL__/stripe-payments-sql-postgres/).
​
Next, you should create an integration between [Shopify and Stitchdata](https://www.stitchdata.com/integrations/shopify/). Click the “Integrations” tab and choose “Shopify”. Enter the details of the integration, including the URL of the Shopify shop:
![](https://lh4.googleusercontent.com/XOGp1kOrSMyR4fmhCYnrxaBiAvUL1YS_JMHb1IVlPNaagyuuWBVpnlnPyvUC_hBT1h0dbxZNtxJALyrEJQXSrRYy5O67_S64t31BVrW4k72oTuUZFZM5W0C-a45alEKpozX3k3HK)

Stitchdata will then begin to extract your Shopify data and load it into Postgres. The process will take less than an hour.
​
## Query your Shopify Data

As Stitchdata loads the data, you can take time to explore its structure. There are different SQL client tools that you can use for this, 
​
Download the right SQL client tool and add your Heroku database to it. You can then use the SQL’s “SELECT *” statement to query the tables created in your database. For example, the following SQL query shows the orders issued by customers:

```
SELECT * FROM shopify.orders;
```

The statement will return the data added to the *orders* table of the *shopify* database.
​
## Visualize the Data
There are different solutions to data visualizations. Arctype is free to use and has build-in dashboard tools to quickly [build visualizations](__GHOST_URL__/sales-dashboard/).
​
Arctype will ask you to establish a [connection to your Heroku Postgres database](https://arctype.com/postgres/setup/heroku-postgres). You can then start to pull data from your table and visualize it.
​
For example, let us create a bar chart that shows the number of items ordered by different clients.
​
First, let us write a SQL query to fetch order names and the corresponding number of items for each order:

```
SELECT name, items FROM orders;
```
The query will return a table similar to this:
![](https://lh6.googleusercontent.com/86ZcM75ydiiZ67puK2PZnpIzHP6zc3EB57kvzK4hP2huOtPXNno3q8TMkaXDGbyKcRj6y0zpNPH6rucq_wDgbmiEhUhBdKQEQcyjwn1Cqy9Pt9b5TWVsfUI1ZMhRZbJJiDA9gQ1o)

To generate a bar chart from this data, click the “Chart” button next to the “Table” button. Next, click the “Select chart type” drop down button on the right and choose “Bar Chart”.
![](https://lh4.googleusercontent.com/Za6bKoUfcdd5-ov7nU2jsdhL02xZZY_57NF_nRwhwjteQRt2yaFckr67vxu_epS5eB7dhOABFxnkNnVpHpwoHvBwaaR3nuKqFqv9vGaewwLeAgR1M0wZhOx9e1rEqSyVCz4E-y3v)

You will see a section written “DRAG COLUMNS HERE” at the bottom right corner of the Arctype screen.
​
Drag one of the columns to the x-axis and the other one to the y-axis. A bar chart will be generated as shown below:
![](https://lh3.googleusercontent.com/_E-hQv_6ieuxD7DAFoAczD0f7Ylbud4-Wl3LUIzAOalGgZwzJPoeQV62X9rmfKVb_E0Vz6HBAI_jLerRWxAHADp9JdJ9ZnIu13klNaErtWnlMJXLJ_YZ_xaLXo5La_bp2tg7EShJ)

To change the type of chart generated, click the drop-down button for “Select chart type” and select the type of chart you want.
​
## Conclusion
The Shopify platform makes it easy for individuals to set up and run online businesses. When running a Shopify business, your store will generate a lot of data. You will want to extract this data and combine it with your other data into a central database to make meaningful decisions about your business. You can use an ETL tool such as Stitchdata to extract your Shopify data and load it to a database of choice such as Postgres. You can then use tools like Arctype to query the data and create visualizations from it.