Practical introduction to relational databases

Fetch only some columns

In the previous section we looked at the generic simple select query, which looked like this. Notice the star:

SELECT * FROM public.products;

That star means everything from the table: all columns of data. Because there's no where clause there (we'll get into that shortly), it will also try to return all rows in the table.

For a practical example here, we will need to copy some data from the users table that supabase itself uses to keep track of the users that interact with its authentication libraries. If you haven't done so, on the example site I had you cloned and spun up, there's a Sign up / Log in button in the top right corner. Sign up or log in. Note that you had to give an email address, and choose a password.

Those details are stored somewhere, but if you look at your tables in the supabase UI (screenshot below), you don't have a users table.

screenshot of supabase ui, red arrow pointing to the table icon that looks like a grid, immediately under the home button, blue arrow pointing to a select element in the sidebar of the table page to choose schema

Supabase has a table called users in the auth schema. Here are a few ways to figure that out.

List a record of all tables in all schemas

Every postgres database will have a few built in schemas and tables or views to hold its own record of what's where: how many tables there are, what those tables look like, what they are called, what constraints exist on them, and so on.

One of the tables and schemas that are common across both MySQL and Postgres is the information_schema concept. To grab a list of all tables in that schema, we can use this query:

SELECT * FROM information_schema.tables where table_schema = 'information_schema';

You can go ahead, open a new query window, paste that in there, and run it. You should get a lot of rows of information along with a LOT of columns. For our purposes we only care about the following columns

Let's suppose we only care about three columns: C2, C3, and C4. These ones:

To achieve that we have to replace the * with the column names we want: C2, C3, and C4.

That will result in a small dataset that is exactly what we want to see in this case:

To translate it back to list the tables from the information_schema view, we can use the following query, which you're welcome to run in your supabase instance:

SELECT table_schema, table_name,  table_type FROM information_schema.tables WHERE table_schema='information_schema';

That will give you every single table and view (see the table_type column) in the information_schema schema.

Because we're going to use this to figure out the table structure of the users table that supabase uses to keep track of, and figure out where it stores when you sign up, modify the query to be this:

SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_schema = 'auth';

You should have a result set that looks something like this:

table_schematable_nametable_type
authschema_migrationsBASE_TABLE
authrefresh_tokensBASE_TABLE
authaudit_log_entriesBASE_TABLE
authinstancesBASE_TABLE
authusersBASE_TABLE

Of these we're only going to work with the users table, but feel free to experiment figuring out what's in the others. As a hint, here's how to get the audit log entries:

SELECT * FROM auth.audit_log_entries;

When you're ready, let's move on to renaming columns in the results.