Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2. The Database: pgstac

pgstac is a PostgreSQL extension that enables STAC metadata management in a PostgreSQL database. eoAPI is useful to many organizations because the other components are configured to work seamlessly with STAC metadata that is stored in your pgstac database.

pypgstac is a Python package for interacting with a pgstac database. You will learn how to use pypgstac to perform the following operations on a pgstac database:

  1. Generate STAC collection record

  2. Add the record to the collections table with Loader.load_collections

  3. Generate STAC item records

  4. Add new records to the items table with Loader.load_items

  5. Delete an item from the items table

For production deployments your STAC metadata generation and ingestion workflow will probably not take place in a notebook but the basic steps will be the same!

Fill in the input boxes below to get started with your own personal Sentinel-2 STAC collection!

  • You will be using the username value to create a unique STAC collection

  • Set a location that is special to you in the lat/lon field - this will determine the spatial extent of your STAC collection

  • You will need to enter the database credentials here in order to post data to the database in this notebook.

Workshop Setup

Run this cell to fetch database credentials. You’ll be prompted to enter the workshop access token provided by your instructor.

Note: API endpoints (STAC, Raster, Vector) are already configured in your environment.

Now you can chose a unique username and a location from which you will generate your own personal Sentinel-2 STAC Collection. Feel free to use the default values, but if you are interested in looking at some satellite imagery from a particular place, this is your chance to pick one!

Source

2.1 Load a collection object

We’ll start by creating a collection, that will eventually contain items, within ~2 degrees of your provided location between January 1 2025 and April 18 2025.

To connect to the pgstac database and upload data pypgstac will use the PG* environment variables that are set in the docker network. With those set you can instantiate a PgstacDB and Loader pair.

The load_collections method accepts an iterable of STAC collection dictionaries, a file path to a collection.json file, or a .ndjson file with multiple collection records. You already have your pystac.Collection in memory in this session so you can just write it to a dictionary and upload it (in a list, i.e. iterable object).

The upsert method will add your collection to the collections table if it does not exist and update any fields with new values if the record does already exist.

Now if you run a query on the database you will find a record for your collection!

2.2 Upload items

You already learned how to create STAC items from scratch in the STAC metadata notebook so you get to take a shortcut for this one. Instead of creating STAC metadata from scratch you can just copy some from an existing STAC collection - this will be sufficient for our exercises during the workshop.

This code will find Sentinel-2 L2A items for the first few months of 2025 that intersect the bounding box of a 2 degree radius around the coordinate that you entered at the top of the notebook.

You are going to upload these items to the items table in the pgstac database but to do so you need to make sure the items’ collection ID matches an existing collection - set it to match the collection that you uploaded in the previous step.

Now you can use the load_items method to upload the list of STAC item dictionaries to the items table in the pgstac database. The insert_ignore method will upload any items that do not exist in the table and skip records that already exist.

Hooray you uploaded some items! Now run a query to verify that the records landed in the items table.

Here is your first glimpse of the power of STAC metadata. You just uploaded these items to the database and now you can browse the data in a beautiful interface without doing any more work!

Radiant Earth built a tool called STAC Browser that is a human-readable interface to a STAC API. Your collection is immediately availble in the STAC API so you can browse it in STAC Browser. Take a tour around the STAC Browser page for your collection.

2.3 Delete an item

It is less common but you might want to delete a STAC record some day. There are not yet any pypgstac functions for doing this but it is relatively easy to do via PostgreSQL query.

Try deleting the last item in the list.

2.4 Search with pypgstac

It is not going to be the best option for searching a pgstac database in most cases (you should probably send a request to stac-fastapi-pgstac), but you can use pypgstac to search for items. You can provide a JSON request body like you would provide to a STAC API in the query arg of PgstacDB.search:

2.5 Aggregations

Since your metadata is now cataloged in a PostgreSQL database, you can summarize it using SQL!