Skip to content

Connecting Superset to a database

Exercise superset-first-connection-annex.pdf · PDF

The first time you play an online game, it asks you things before it lets you in. Which server? Which world? Your account? Your password? Get one wrong and you stay in the lobby, staring at an error message.

A database does exactly the same. On the last page you saw that, of the six kinds of data source, the relational database is the one that asks for the most before it opens. Today you learn what it asks for, why, and how to read the error when the door stays shut.

Then you walk through that door: you put a real dataset into the database and ask it your first question.

What you will learn

  • Name the five things a database connection needs, plus the driver, and say what each one is for
  • Read a connection string and find each of those parts inside it
  • Tell from an error message which setting is wrong
  • Load a CSV file into a database from Superset
  • Run your first queries in SQL Lab
  • Avoid the trap: thinking that Superset stores your data, or that localhost always means "my computer"

Superset does not hold your data

Start with the picture, because everything else hangs from it.

flowchart LR
    B["<b>Your browser</b><br>what you see and click"] -- "web page" --> S["<b>Superset</b><br>(the game client)<br>connects, asks, draws"]
    S -- "SQL, through<br>the driver" --> D[("<b>PostgreSQL</b><br>(the game server)<br>where the rows live")]
    D -- "rows" --> S

There are three players here, and they are three different programs:

Program In the game What it does What it stores
Your browser The screen Shows Superset's pages Nothing
Superset The game client Connects to the database, sends questions, draws the answers Its own settings: users, saved connections, charts. Not your rows.
PostgreSQL The game server Keeps the tables and answers questions about them Your data

A database server is a program that keeps data in tables and answers questions about them for any program that connects with the right details. PostgreSQL is the one we use.

A connection is the set of details one program needs to reach a database server and be let in.

Superset is a client. A game client with no server to join shows you a pretty menu and nothing else. So the first job in Superset is always the same: tell it where the server is.

Careful: \"Superset stores my data\"

It does not. Superset stores connections and charts. The rows are in PostgreSQL. If Superset disappeared tomorrow, your tables would still be there, and any other tool with the right connection details could read them. You will see that for yourself at the end of this unit, with Power BI.

Five things and a translator

Every database connection, in every tool you will ever use, asks for the same five things. Learn them once.

Setting The question it answers In the game Looks like
Host Which machine is the database on? Which server you join A name or a number: db.example.com
Port Which door of that machine? Which door of that server A number: 5432
Database Which database on that server? Which world on that server A name: shop
User Who are you? Your account A name: your_user
Password Can you prove it? Your account's password Secret. Never shown on a page like this one.

Three of those words are new:

  • The host is the network name or address of the machine where the database server runs.
  • A port is a numbered door on a machine. One machine can run many programs that listen to the network, and each one waits behind its own number. PostgreSQL's usual door is 5432. Superset's own web page uses a different one, 8088: do not mix them up.
  • One database server can hold several separate databases, like one game server hosting several worlds. You must say which one you want.

And then there is a sixth thing, which no form asks you to type:

A driver is a small piece of software that lets one program talk to one specific kind of database.

Superset speaks SQL. But PostgreSQL, MySQL and the rest each expect that SQL to arrive over the network in their own way. The driver is the translator. Superset's driver for PostgreSQL is called psycopg2; for MySQL it would need a different one.

Careful: \"the password is right, so it must connect\"

Not without the driver. If the translator is not installed, Superset cannot say a single word to that database, however right the password is. In Superset you notice it early: a database whose driver is missing does not appear in the list of databases you can connect to.

All five in one line: the connection string

Typing five boxes is one way to give the details. The other is one line of text.

A connection string is a single line that holds every detail of a connection, in a fixed order.

For PostgreSQL in Superset it has this pattern:

postgresql://USER:PASSWORD@HOST:PORT/DATABASE

With invented values, so you can see where each part goes:

postgresql://your_user:your_password@db.example.com:5432/shop
Piece Setting
postgresql:// The kind of database, which tells Superset which driver to use
your_user User
your_password Password
db.example.com Host
5432 Port
shop Database

Read the punctuation and you can decode any connection string: :// after the kind of database, : between user and password, @ before the host, : before the port, / before the database.

A connection string is a password

Look at the second example again: the password is right there, in plain text. A connection string is a key to the whole database. Never paste one into a chat, a screenshot, a forum question or a file you share. The values on this page are invented on purpose; yours are given to you in class and stay there.

Where it goes in Superset

In Superset, a connection is created once and then everybody who uses that Superset works through it. On your class VM it already exists, under the name Dades classe. This is how one is made, so you know what you are looking at.

Step Do this You should see
1 Settings → Database Connections The list of connections Superset knows
2 + Database, then choose PostgreSQL A form with the boxes Host, Port, Database name, Username, Password and Display Name
3 Fill in the five settings, and a Display Name: the label people will see
4 Press Connect Superset tries the connection. If a detail is wrong, an error appears here.
5 Edit the connection (pencil icon) → Advanced → Security → tick Allow file uploads to database → Finish In the list: the connection, its backend postgresql, and a tick under File upload

Step 5 is not one of the five settings. It is a permission: a switch on Superset's side that decides what people may do through this connection. Without it, nobody can load a file into that database from Superset.

So the full list for "configuring access" is: host, port, database, user, password, driver, and permissions.

Reading the error

Connections fail. That is normal, and the error message is not noise: it tells you how far you got.

What you see How far you got The setting to check
The kind of database is not in the list Nowhere: no translator Driver
A message containing Connection refused You knocked, and nobody was behind that door Host or port
A message saying the password for your user is incorrect You reached the server, and it said no User or password
The connection works, but it is missing from the upload form You are in, but not allowed to do that Permission: Allow file uploads
The upload works, but the table has one single column Not a connection problem at all The file's separator

Which is better news: connection refused or wrong password? Wrong password. It means the host and the port were right and a server answered. You are one typo away. Connection refused means you have not even found the server yet.

Careful: localhost is not always your computer

localhost is a special host name that means "this same machine": the machine where the program that uses the word is running. You may also see it written as the number 127.0.0.1.

When you type localhost in your browser, it means your computer. But in a connection form, the program that will use the word is Superset, and Superset runs on a server. So localhost there means "look for the database inside the machine where Superset itself runs". If the database is somewhere else, Superset knocks on its own door, finds nobody, and reports connection refused.

In the game: "this console" is always your own. To reach a friend's, you need their gamertag. The host is the gamertag.

So when you see connection refused, do not conclude that the database is switched off. Usually it is on, and the host is wrong.

Loading a CSV into the database

On the last page you downloaded pokemon_df.csv. A file on your disk is loot in your backpack. To query it with SQL, it has to become a table in the database.

Superset can do that for you: it reads the file and creates the table in PostgreSQL, through the connection.

Step Do this You should see
1 + (top right) → Data → Upload CSV to database The upload form
2 CSV file: choose pokemon_df.csv
3 Database: Dades classe If it is not in the list, the Allow file uploads permission is missing
4 Schema: public. Never leave it empty. Left empty, the upload fails with schema "undefined" does not exist
5 Table name: pokemon. Short, lowercase, no spaces.
6 Delimiter: check it against the file. Ours uses a comma.
7 Upload Datasets now lists pokemon

Two new words:

  • A schema is a folder of tables inside a database. public is the one PostgreSQL gives you from the start.
  • A dataset is Superset's name for "something I can build charts from". Here it is a label pointing at your new table. You will meet datasets properly in Datasets and destination.

Step 6 is where the previous page pays off. If your file uses ; between values and the form still says ,, the upload "works" and you get a table with one single, very wide column. And if the numbers use a decimal comma, set File settings → Decimal character to , as well.

Careful: \"the upload broke my data\"

It did not. The file is fine; the tool was told the wrong separator or decimal mark. Delete the table and upload again with the right settings. Nothing in the original file has changed.

One thing the upload does quietly: in our file, a missing value is written as the two letters NA. Superset loads those cells as NULL, the database's way of saying "there is no value here". Charmander has only one type, so its type_2 is NULL. That is your first taste of missing values, the big topic of UT2.

First questions in SQL Lab

SQL (Structured Query Language) is the language for asking questions to a relational database. One question is called a query.

SQL Lab is the page of Superset where you write a query, run it and see the answer as a table.

Open SQL → SQL Lab. On the left choose the database Dades classe and the schema public. Then ask three questions, one at a time, pressing Run after each.

How many rows are there?

SELECT COUNT(*) FROM pokemon;
count
949

SELECT means "show me", FROM pokemon says which table, and COUNT(*) counts rows. 949 Pokémon: the same number you counted in the text editor. The whole file arrived.

What do the rows look like?

SELECT * FROM pokemon LIMIT 10;

The * means "every column" and LIMIT 10 means "stop after ten rows". You get 10 rows and 22 columns. These are the first four rows and some of the columns:

id pokemon height weight type_1 type_2 hp attack
1 bulbasaur 0.7 6.9 grass poison 45 49
2 ivysaur 1 13 grass poison 60 62
3 venusaur 2 100 grass poison 80 82
4 charmander 0.6 8.5 fire NULL 39 52

Always run this one on a table you have never seen. It costs a second and it shows you the column names, the kind of value in each one, and surprises like that NULL.

Which type hits hardest?

SELECT type_1, COUNT(*) AS n, ROUND(AVG(attack)) AS avg_attack
FROM pokemon
GROUP BY type_1
ORDER BY avg_attack DESC;

The answer has 18 rows, one per type. The first five:

type_1 n avg_attack
dragon 39 109
fighting 31 99
ground 36 96
steel 30 93
rock 65 90

Read it as a sentence: for each main type, count the Pokémon and average their attack, then sort from the highest average down. Dragon types hit hardest, with an average attack of 109. The most common type, water, has 126 Pokémon but sits far down the list with an average of 75.

Do not worry if that third query looks like a lot. GROUP BY is the only really new idea in it, and it gets its own page: SQL Lab: groups.

Notice what just happened. In the crafting game, this was the whole of stage 1: you found the loot (a CSV on a website), carried it home (the upload) and stored it where you can work with it (a table). And you asked a question a text editor could never answer.

Try it: your own first connection

On your class VM, with Superset open in the browser:

Step Do this You should see
1 Open Settings → Database Connections and find Dades classe Backend postgresql, and a tick under File upload
2 Open Datasets. If pokemon is not listed, upload pokemon_df.csv as explained above. pokemon in the list
3 In SQL Lab, run the three queries of this page 949; ten rows; dragon on top with 109
4 Change the third query to use type_2 instead of type_1 (in all three places) A different ranking, with fighting on top at 112, and one row where the type is NULL
5 Write down, without looking: the five settings of a connection, and the sixth thing Host, port, database, user, password; and the driver

If you downloaded your own dataset in the open-data hunt, upload it too, with its own table name, and adapt the third query to the question you wrote on your card.

Key takeaways

  • Superset is a client: it connects, asks and draws. Your rows live in the database server (PostgreSQL), not in Superset.
  • A connection needs five things: host, port, database, user and password. PostgreSQL's usual port is 5432.
  • The driver is the translator between a tool and one kind of database. No driver, no connection, even with the right password.
  • A connection string packs all of it into one line, password included. Treat it as a secret.
  • Error messages say how far you got: connection refused points at host or port; incorrect password means you already reached the server.
  • localhost means "the machine where this program runs". For Superset, that is its own server, not your computer.
  • Uploading a CSV creates a real table in the database. Check schema and delimiter in the form. Then ask it questions in SQL Lab.

Quick check

1. Which list contains the five settings of a database connection?
  • Host, port, driver, browser, password
  • Host, port, database, user, password
  • File, separator, encoding, sheet, header
  • Server, schema, table, column, row

Host (which machine), port (which door), database (which world), user and password (your account). The driver is the sixth thing, but you do not type it in the form: it is software that must be installed.

2. What is the driver for?
  • It stores the password so that you do not have to type it again
  • It makes queries run faster
  • It lets a program talk to one specific kind of database
  • It converts CSV files into tables

The driver is the translator. Superset speaks SQL, but each kind of database expects it in its own way over the network. Without the right driver, Superset cannot talk to that database at all.

3. In the connection string postgresql://ana:s3cret@data.example.org:5432/library, what is the host?
  • ana
  • library
  • data.example.org
  • postgresql

The pattern is postgresql://USER:PASSWORD@HOST:PORT/DATABASE. The host comes after the @ and before the : of the port. Here the user is ana, the port is 5432 and the database is library. And yes: the password is readable in the middle, which is why a connection string is a secret.

4. Superset shows a message saying that the password for your user is incorrect. What does that tell you?
  • The database server is switched off
  • The driver is not installed
  • The host and the port are right: you reached the server and it rejected the login
  • The CSV file has the wrong separator

To reject a password, a server first has to answer. So host and port are fine, and the problem is in the user or the password. That is better news than connection refused, where you have not found the server yet.

5. Where are the 949 Pokémon rows stored after you upload the CSV from Superset?
  • Inside Superset, next to the charts
  • In your browser
  • In a table in the PostgreSQL database
  • Only in the original CSV file

Superset reads the file and creates a table in the database, through the connection. Superset itself keeps only its settings: users, connections and charts. Any other tool with the right connection details could read that table.

6. What does SELECT COUNT(*) FROM pokemon; return?
  • The first row of the table
  • The names of the columns
  • One number: how many rows the table has
  • Every row of the table

COUNT(*) counts rows, so the answer is a single number: 949. To see rows you would write SELECT * FROM pokemon LIMIT 10;.

7. A friend uses Superset on one server to reach a MySQL database on another server called botiga. The host box says 127.0.0.1 and Superset answers connection refused. Pick the two statements that are true.
  • 127.0.0.1 means "this same machine", so Superset is looking for MySQL on its own server; the host should be botiga
  • The password is wrong: connection refused always means a bad password
  • Superset also needs a MySQL driver installed, or it cannot talk to MySQL at all
  • MySQL cannot be used from Superset, only PostgreSQL

127.0.0.1 is localhost written as a number: the machine where the program runs. Superset knocked on its own door. Connection refused points at host or port, not at the password. And each kind of database needs its own driver: the PostgreSQL one does not speak MySQL.

The door is open and the table is in. So far you asked for everything; next you learn to ask for exactly the rows you need: SQL Lab: filters.