Skip to content

Commit 3f539f1

Browse files
committed
feat: support standard Postgres env vars for db connection
Support DATABASE_URL, PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE environment variables for configuring the database connection. Env vars take highest priority, overriding CLI args, LSP settings, and config file values. Closes #302
1 parent 371bded commit 3f539f1

4 files changed

Lines changed: 65 additions & 15 deletions

File tree

crates/pgls_configuration/src/database.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ impl PartialDatabaseConfiguration {
7373
pub fn from_env() -> Option<Self> {
7474
let database_url = std::env::var("DATABASE_URL").ok();
7575
let pghost = std::env::var("PGHOST").ok();
76-
let pgport = std::env::var("PGPORT")
77-
.ok()
78-
.and_then(|p| p.parse().ok());
76+
let pgport = std::env::var("PGPORT").ok().and_then(|p| p.parse().ok());
7977
let pguser = std::env::var("PGUSER").ok();
8078
let pgpassword = std::env::var("PGPASSWORD").ok();
8179
let pgdatabase = std::env::var("PGDATABASE").ok();

crates/pgls_env/src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,9 @@ impl PgLSEnv {
7676
"DATABASE_URL",
7777
"A connection string that encodes the full database connection setup.",
7878
),
79-
pghost: PgLSEnvVariable::new(
80-
"PGHOST",
81-
"The host of the database server.",
82-
),
83-
pgport: PgLSEnvVariable::new(
84-
"PGPORT",
85-
"The port of the database server.",
86-
),
87-
pguser: PgLSEnvVariable::new(
88-
"PGUSER",
89-
"The username to connect to the database.",
90-
),
79+
pghost: PgLSEnvVariable::new("PGHOST", "The host of the database server."),
80+
pgport: PgLSEnvVariable::new("PGPORT", "The port of the database server."),
81+
pguser: PgLSEnvVariable::new("PGUSER", "The username to connect to the database."),
9182
pgpassword: PgLSEnvVariable::new(
9283
"PGPASSWORD",
9384
"The password to connect to the database.",

docs/codegen/src/env_variables.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ pub fn generate_env_variables(docs_dir: &Path) -> Result<()> {
3939
env.pgls_config_path.description()
4040
)?;
4141

42+
writeln!(
43+
content,
44+
"### `{}`\n\n {}\n",
45+
env.database_url.name(),
46+
env.database_url.description()
47+
)?;
48+
writeln!(
49+
content,
50+
"### `{}`\n\n {}\n",
51+
env.pghost.name(),
52+
env.pghost.description()
53+
)?;
54+
writeln!(
55+
content,
56+
"### `{}`\n\n {}\n",
57+
env.pgport.name(),
58+
env.pgport.description()
59+
)?;
60+
writeln!(
61+
content,
62+
"### `{}`\n\n {}\n",
63+
env.pguser.name(),
64+
env.pguser.description()
65+
)?;
66+
writeln!(
67+
content,
68+
"### `{}`\n\n {}\n",
69+
env.pgpassword.name(),
70+
env.pgpassword.description()
71+
)?;
72+
writeln!(
73+
content,
74+
"### `{}`\n\n {}\n",
75+
env.pgdatabase.name(),
76+
env.pgdatabase.description()
77+
)?;
78+
4279
writeln!(
4380
content,
4481
"### `{}`\n\n {}\n",

docs/reference/env_variables.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@
1919

2020
A path to the configuration file
2121

22+
### `DATABASE_URL`
23+
24+
A connection string that encodes the full database connection setup.
25+
26+
### `PGHOST`
27+
28+
The host of the database server.
29+
30+
### `PGPORT`
31+
32+
The port of the database server.
33+
34+
### `PGUSER`
35+
36+
The username to connect to the database.
37+
38+
### `PGPASSWORD`
39+
40+
The password to connect to the database.
41+
42+
### `PGDATABASE`
43+
44+
The name of the database to connect to.
45+
2246
### `PGT_LOG_PATH`
2347

2448
The directory where the Daemon logs will be saved. Deprecated, use PGLS_LOG_PATH instead.

0 commit comments

Comments
 (0)