Go SQLDB Postgres 🐘
This example is a WebAssembly component that can query a PostgresDB database.
The application...
- Implements the 
wasmcloud:postgresWIT contract - Uses the 
sqldb-postgres-providercapability provider - Can be declaratively provisioned in a wasmCloud environment
 
📦 Dependencies
[!WARNING] Due to incompatibilities introduced in
wasm-toolsv1.226.0, a version ofwasm-tools<= 1.225.0 is required for running this example.You can install
wasm-toolsv1.225.0 from upstream releases, or usecargo(Rust toolchain) -- (i.e.cargo install --locked wasm-tools@1.225.0)
Before starting, ensure that you have the following installed in addition to the Go (1.23+) toolchain:
tinygofor compiling Go (always use the latest version)wasm-tools(<=1.225.0) for Go bindings- wasmCloud Shell (
wash) for building and running the components and wasmCloud environment dockerfor 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.gitChange directory to examples/component/sqldb-postgres-query:
cd examples/component/sqldb-postgres-queryIn addition to the standard elements of a Go project, the example directory includes the following files and directories:
build/: Target directory for compiled.wasmbinariesgen/: 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 buildThis 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.gzfor your provider.
Start a wasmCloud environment
Start a local wasmCloud environment (using the -d/--detached flag to run in the background):
wash up -dDeploy the application
wash app deploy wadm.yamlTo ensure that the application has reached Deployed status, you can use wash app list:
wash app listInvoke 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.callNote 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.yamlStop your local wasmCloud environment:
wash downRemember 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.