Quickstart: Use Data API builder with PostgreSQL
In this Quickstart, you build a set of Data API builder configuration files to target a local PostgreSQL database.
Prerequisites
- Docker
- .NET 8
- A data management client
- If you don't have a client installed, install Azure Data Studio
Tip
Alternatively, open this Quickstart in GitHub Codespaces with all developer prerequisites already installed. Simply bring your own Azure subscription. GitHub accounts include an entitlement of storage and core hours at no cost. For more information, see included storage and core hours for GitHub accounts.
Install the Data API builder CLI
Install the Microsoft.DataApiBuilder
package from NuGet as a .NET tool.
Use
dotnet tool install
to install the latest version of theMicrosoft.DataApiBuilder
with the--global
argument.dotnet tool install --global Microsoft.DataApiBuilder
Note
If the package is already installed, you will update the package instead using
dotnet tool update
.dotnet tool update --global Microsoft.DataApiBuilder
Verify that the tool is installed with
dotnet tool list
using the--global
argument.dotnet tool list --global
Configure the local database
Start by configuring and running the local database. Then, you can seed a new container with sample data.
Get the latest copy of the
postgres:16
container image from Docker Hub.docker pull postgres:16
Start the docker container by setting the password and publishing port 5432. Replace
<your-password>
with a custom password.docker run \ --publish 5432:5432 \ --env "POSTGRES_PASSWORD=<your-password>" \ --detach \ postgres:16
Connect to your local database using your preferred data management environment. Examples include, but aren't limited to: pgAdmin, Azure Data Studio, and the PostgreSQL extension for Visual Studio Code.
Tip
If you're using default networking for your Docker Linux container images, the connection string will likely be
Host=localhost;Port=5432;User ID=postgres;Password=<your-password>;
. Replace<your-password>
with the password you set earlier.Create a new
bookshelf
database.DROP DATABASE IF EXISTS bookshelf; CREATE DATABASE bookshelf;
Create a new
dbo.authors
table and seed the table with basic data.CREATE TABLE IF NOT EXISTS authors ( id INT NOT NULL PRIMARY KEY, first_name VARCHAR(100) NOT NULL, middle_name VARCHAR(100), last_name VARCHAR(100) NOT NULL ); INSERT INTO authors VALUES (01, 'Henry', NULL, 'Ross'), (02, 'Jacob', 'A.', 'Hancock'), (03, 'Sydney', NULL, 'Mattos'), (04, 'Jordan', NULL, 'Mitchell'), (05, 'Victoria', NULL, 'Burke'), (06, 'Vance', NULL, 'DeLeon'), (07, 'Reed', NULL, 'Flores'), (08, 'Felix', NULL, 'Henderson'), (09, 'Avery', NULL, 'Howard'), (10, 'Violet', NULL, 'Martinez');
Create configuration files
Create a baseline configuration file using the DAB CLI. Then, add a development configuration file with your current credentials.
Create a typical configuration file using
dab init
. Add the--connection-string
argument with your database connection string from the first section. Replace<your-password>
with the password you set earlier in this guide. Also, add theDatabase=bookshelf
value to the connection string.dab init --database-type "postgresql" --host-mode "Development" --connection-string "Host=localhost;Port=5432;Database=bookshelf;User ID=postgres;Password=<your-password>;"
Add an Author entity using
dab add
.dab add Author --source "public.authors" --permissions "anonymous:*"
Test API with the local database
Now, start the Data API builder tool to validate that your configuration files are merged during development.
Use
dab start
to run the tool and create API endpoints for your entity.dab start
The output of the tool should include the address to use to navigate to the running API.
Successfully completed runtime initialization. info: Microsoft.Hosting.Lifetime[14] Now listening on: <http://localhost:5000> info: Microsoft.Hosting.Lifetime[0]
Tip
In this example, the application is running on
localhost
at port 5000. Your running application may have a different address and port.First, try the API manually by issuing a GET request to
/api/Author
.Tip
In this example, the URL would be
https://localhost:5000/api/Author
. You can navigate to this URL using your web browser.Next, navigate to the Swagger documentation page at
/swagger
.Tip
In this example, the URL would be
https://localhost:5000/swagger
. Again, you can navigate to this URL using your web browser.