Get started with Apache Superset and PostgreSQL
"A picture is worth a thousand words" is a common saying in many languages. Similarly, in the data world, a visualization condenses huge amounts of raw points into an image that can be used to create insightful stories driving business decisions. Several tools exist in the dataviz ecosystem, with Apache Superset getting a lot of traction for its open source status and the advanced list of features it offers for data exploration, visualization and sharing.
This blog post shows how to start creating amazing visualizations with Apache Superset building on an existing PostgreSQL table.
Get your PostgreSQL free plan todayReady to dive deeper into PostgreSQL? Sign up for a free PostgreSQL plan and follow along.
Run Apache Superset on Docker
Docker is a good friend here! It allows us to spin up - in a matter of minutes - complex software infrastructure without needing to understand and implement the setup manually. In cases like this when we're testing a new tool, it's a perfect fit!
We can start by cloning the Apache Superset GitHub repo, and navigating to the
superset folder from our terminal:
git clone https://github.com/apache/superset.git cd superset
Then we can use
docker-compose to create all the containers needed for Apache Superset:
docker-compose -f docker-compose-non-dev.yml up
Boom! Our Apache Superset is ready with a nice set of pre-built content we can browse.
Now, let's go to
localhost:8088 in our browser, and login with the safest duo ever (
admin as user, and
admin as password).
If you're curious, check out the Apache Superset docker page for more information.
Push the dataset to PostgreSQL
We've explored happiness before on the blog, and how to push Kaggle's related dataset to PostgreSQL via Python. You can use the output mentioned there for the current example, or use your own. The end result in our example is a PostgreSQL instance named
demo-pg, containing a table called
happiness with content similar to the one below:
survey_yr | country | overall_rank | score | gdp | generosity | freedom | social_support | life_exp | gov_trust ----------+---------+--------------+---------+--------+------------+----------+----------------+----------+---------- 19 | Finland | 1 | 7.7690 | 1.3400 | 0.1530 | 0.5960 | 1.587 | 0.9860 | 0.3930 19 | Denmark | 2 | 7.60 | 1.3830 | 0.2520 | 0.5920 | 1.573 | 0.9960 | 0.4100 19 | Norway | 3 | 7.554 | 1.4880 | 0.2710 | 0.6030 | 1.582 | 1.0280 | 0.3410
Connect Apache Superset to PostgreSQL
All we need to do is go to the Apache Superset UI, and define a new datasource pointing to our PostgreSQL database:
- Select Data, and then Databases.
- Click the +DATABASE button.
- At the bottom of the modal window, choose Connect this database with a SQLAlchemy URI string instead.
demo-pgas DISPLAY NAME (or your own instance name).
- Use the PostgreSQL URI as SQLALCHEMY URI. You can find the PostgreSQL URI in Aiven's console, in the service detail view, under the Overview tab.
- Test that all the settings are correct by checking your connection, and making sure you get a nice
Connection looks good!message.
- Click ADD to persist our
Now, it's time to include the
- Back in the Apache Superset UI, switch to the Datasets tab.
- Create a table by filling the DATASOURCE with
demo-pg, the SCHEMA with
publicand the table with - no surprises -
- Click ADD to persist the datasource definition.
Now we are ready to use our artistic skills, and create visualizations of our data: representing data this way makes it much easier to process.
- Back in the Apache Superset UI, let's navigate to Charts tab.
- Click +CHART.
- In the popup window, select the
public.happinessdataset we created before, and Pivot Table v2 as visualization.
Let's use the dataset, and create a heatmap of the top 10 happiest countries over the years. We can do that with the following configuration:
countryfor the ROWS
survey_yrfor the COLUMNS
- For the METRIC select a SIMPLE calculation based on the
AVGas aggregated function.
- In the FILTERS section, add a SIMPLE filter based on the
- At the bottom, set the AGGREGATION FUNCTION as
Average, this will drive the overall row/column aggregation in the pivot table.
Check out the Query parameters overview:
Now, in the CUSTOMIZE tab set the following options:
- For PIVOT TABLE TYPE select
- For the ROWS SORT BY select
- Deselect the SHOW COLS TOTALS
- Name the visualization
Country ranking heatmap
We're ready to press the RUN button above CUSTOMIZE.
Aaaaand, we end up with a lovely red shaded heatmap showing the countries that made the top 10 at least once in the previous 5 years, ordered by their overall average position.
To see where these countries are around the world, the same data could be used to create a map. Try the following settings:
- World Map as visualization type
countryas COUNTRY COLUMN
Full Nameas COUNTRY FIELD TYPE
- Use a SIMPLE calculation based on the
AVGaggregation for the METRIC FOR COLOR parameter
- Choose the COUNTRY COLOR SCHEME, the screenshot shows
red/yellow/bluebut the choice is yours. Go wild!
The resulting map clearly indicates that a lot of work needs to be done to raise happiness levels in the Southern Hemisphere.
Add calculated fields in the dataset
Sometimes the source dataset doesn't contain all the required fields. This is the case in our example too, with the
survey_yr field containing only the last two digits of the year (ie.
19), which means it won't be recognized by Apache Superset as a timestamp, stopping us from using any trend visualization.
Fear not! Apache Superset allows us to change the shape of our dataset, without any modification to the original table, by adding calculated columns.
- In the Apache Superset UI, switch to the Datasets panel.
- Click the Edit pencil icon under the Action section of the happiness dataset.
- Open the CALCULATED COLUMNS tab and create a new item, with the following settings:
2000+survey_yras SQL EXPRESSION
* LABEL set to
DATETIMEas DATA TYPE
%Yas DATETIME FORMAT
* Is temporal checkbox selected.
Year column is available, enabling us to create trend visualizations like a linechart of the top 5 positions over time.
Challenge yourself, and try creating this chart yourself as an Apache Superset.
As you'll see, Finland is a pretty happy place - and we think so too!
A dataset is only useful when stored properly, and made available for queries. It becomes meaningful when insights are discovered and communicated across the company. The combination of PostgreSQL and Apache Superset offers a best-in-class solution for data storage, discovery and visualization, enabling companies to be effective and data-driven.
Some more info:
- Aiven for PostgreSQL: to get your managed PostgreSQL instance
- Apache Superset Documentation: to browse the configuration details
Not using Aiven services yet? Sign up now for your free trial at https://console.aiven.io/signup!
Sep 8, 2022
Working with command line tools and SQL can be intimidating. Read on to learn how to use the pgweb GUI to provide useful views of PostgreSQL® data.
Sep 27, 2022
By combining and correlating data from any source in one dashboard, Grafana® ensures your team is always on the same page.
Sep 13, 2022
There's more than one way to use Aiven. Read on to learn how to use the Aiven command line tool to do common tasks.
Subscribe to the Aiven newsletter
All things open source, plus our product updates and news in a monthly newsletter.