PostgreSQL samples to a feature tensor
Query four synthetic classes with native libpq, bound values and private credentials, then create a bounded DataTable and numeric feature tensor.
// Replace these public placeholders and configure example-postgres-password privately.
import aner.db;
import aner.db.postgres;
import aner.secrets;
import aner.data;
import aner.tensor;
let connection: DBConnection = postgres.connect(
host: "postgres.example.invalid", port: 5432, database: "example_science",
user: "example_reader", password: secrets.stored("example-postgres-password"),
sslmode: "verify-full", timeout_seconds: 10);
// Run examples/fixtures/db_mock.sql once with your database administration tool.
// SQL and parameters travel separately; never interpolate user input into SQL.
let result: DBResult = db.query(connection,
"SELECT sample_id, feature_a, feature_b, feature_c, label FROM aner_demo.samples WHERE label = $1::integer ORDER BY sample_id",
List("2"), max_rows: 1000, max_bytes: 1048576);
print(db.rows(result));
if db.rows(result) > 0 {
let table: DataTable = db.to_table(result, 1048576);
print(data.integer(table, 0, "label"));
}
// Select numeric features in SQL to use data.to_tensor with no string/null columns.
let features = db.query(connection,
"SELECT feature_a, feature_b, feature_c FROM aner_demo.samples ORDER BY sample_id",
List(), max_rows: 1000, max_bytes: 1048576);
let matrix = data.to_tensor(db.to_table(features, 1048576), 1048576);
print(tensor.rows(matrix));
print(tensor.cols(matrix));
db.close(connection);
Make it your experiment.
With Aner installed, save this program as examples/db_postgres.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/db_postgres.aner
aner run examples/db_postgres.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Use a compatible installed Aner executable with PostgreSQL libpq 14 or newer. Check db.available for the selected connector. In a dedicated development database, run db_mock.sql once with your database administration tool before running Aner. Replace the fictional host, database and username privately. Store the password with the credential setup commands below, or select an explicit environment/file reference as explained in the connector and Credentials guides. Configure a trusted CA and a hostname matching the server certificate. The examples keep verified TLS enabled. Checking the source opens no database connection; running connects to your configured server.
aner secrets set example-postgres-password --store system
aner secrets status example-postgres-password --store systemBundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution