Go SQLDB Postgres 🐘
This example is a WebAssembly component that can query a PostgresDB database.
The application...
- Implements the
wasmcloud:postgres
WIT contract - Uses the
sqldb-postgres-provider
capability provider - Can be declaratively provisioned in a wasmCloud environment
📦 Dependencies
Before starting, ensure that you have the following installed in addition to the Go (1.23+) toolchain:
tinygo
for compiling Go (always use the latest version)wasm-tools
for Go bindings- wasmCloud Shell (
wash
) for building and running the components and wasmCloud environment docker
for easily running instances of [postgres
]
🐘 Start a local Postgres cluster
Before we can connect to a Postgres database cluster, we'll need to have one running. You can start one quickly with docker
:
docker run \
--rm \
-e POSTGRES_PASSWORD=postgres \
--name pg -p 5432:5432\
postgres:16.2-alpine
👟 Run the example
Clone the wasmCloud/go repository:
git clone https://github.com/wasmCloud/go.git
Change directory to examples/component/sqldb-postgres-query
:
cd examples/component/sqldb-postgres-query
In addition to the standard elements of a Go project, the example directory includes the following files and directories:
build/
: Target directory for compiled.wasm
binariesgen/
: Target directory for Go bindings of interfaceswit/
: Directory for WebAssembly Interface Type (WIT) packages that define interfacesbindings.wadge_test.go
: Automatically generated test bindingswadm.yaml
: Declarative application manifestwasmcloud.lock
: Automatically generated lockfile for WIT packageswasmcloud.toml
: Configuration file for a wasmCloud application
Build the component
We will build and deploy this example manually, since we will be using the wash call
subcommand to interact with the application, requiring a stable identity to call. (As you gain experience with wasmCloud, you will likely want to use the wash dev
subcommand to automate your development process.)
Build the component:
wash build
This will create a folder called build
which contains sqldb-postgres-query_s.wasm
.
⚠️ If you're using a local build of the provider (using
file://...
inwadm.yaml
) this is a good time to ensure you've built the provider archivepar.gz
for your provider.
Start a wasmCloud environment
Start a local wasmCloud environment (using the -d
/--detached
flag to run in the background):
wash up -d
Deploy the application
wash app deploy wadm.yaml
To ensure that the application has reached Deployed
status, you can use wash app list
:
wash app list
Invoke the component
Once the component and provider are deployed, you can invoke the example component with the wash call
subcommand, which invokes a function on a component:
wash call go_sqldb_postgres_query-querier wasmcloud:examples/invoke.call
Note that the name of the component is prefixed with the application name (specified in the application manifest (wadm.yaml
) metadata), and the interface on it we call is defined in wit/provider.wit
(the call
function of the invoke
interface).
Clean up
You can delete an application from your wasmCloud environment by referring either to its application name (sqldb-postgres-query
) or the original application manifest:
wash app delete wadm.yaml
Stop your local wasmCloud environment:
wash down
Remember to stop your PostgresDB container as well.
Running tests
You can also run tests using wadge. Because this example uses a custom interface (wasmcloud:postgres
), there is a test harness component in the test-harness
directory of this example. You can modify this harness (or create another one for other test cases). If you change the harness, you'll need to rm build/test-harness.wasm
. When you run wash build
again it will build the test harness component. Alternatively, you can run go generate ./...
to regenerate the test harness.
📖 Further reading
For more on building components, see the Component Developer Guide in the wasmCloud documentation.