mz_deploy/cli/commands/
sql.rs1use std::io;
13use std::os::unix::process::CommandExt;
14use std::process::Command;
15
16use crate::cli::CliError;
17use crate::client::{build_options_string, default_sslmode};
18use crate::config::Settings;
19
20const APPLICATION_NAME: &str = "mz-deploy-sql";
21
22pub fn run(settings: &Settings, psql_args: Vec<String>) -> Result<(), CliError> {
32 let profile = settings.connection();
33 let host = profile.require_host()?;
34
35 let mut cmd = Command::new("psql");
36 cmd.env("PGHOST", host);
37 cmd.env("PGPORT", profile.port.to_string());
38 cmd.env("PGUSER", &profile.username);
39 cmd.env("PGDATABASE", "materialize");
40 if let Some(password) = &profile.password {
41 cmd.env("PGPASSWORD", password);
42 }
43
44 let mode = profile.sslmode.unwrap_or_else(|| default_sslmode(host));
45 cmd.env("PGSSLMODE", mode.libpq_name());
46 if let Some(cert) = &profile.sslrootcert {
47 cmd.env("PGSSLROOTCERT", cert);
48 }
49
50 if let Some(options) = build_options_string(&profile.options) {
51 cmd.env("PGOPTIONS", options);
52 }
53
54 cmd.env("PGAPPNAME", APPLICATION_NAME);
55 cmd.args(psql_args);
56
57 let err = cmd.exec();
61 let msg = if err.kind() == io::ErrorKind::NotFound {
62 "failed to launch psql: binary not found on PATH. \
63 Install it with `brew install libpq` (macOS) or \
64 `apt install postgresql-client` (Debian/Ubuntu)."
65 .to_string()
66 } else {
67 format!("failed to launch psql: {err}")
68 };
69 Err(CliError::Message(msg))
70}