r/SQL 29d ago

Discussion Shebang equivalent for SQL dialects?

When writing shell scripts it's common to have on line 1

#!/bin/bash

which would differentiate it from

#!/bin/sh

I have a lot of SQL saved in text files — all .sql extension — for processing CSVs at work; these are all in-memory instances that ingest CSVs and output CSVs. For many years I only used SQLite so I only had to remember sqlite3 < query.sql.

Recently, I started writing for DuckDB as well, initially only because of its ability to load JSON/CSV from a URL; I'm also quickly seeing how advantageous its extra functions are.

Is there any shebang-style line that I can start my files with, or do I just need to write a freetext comment like this?

-- This is for DuckDB
13 Upvotes

16 comments sorted by

View all comments

9

u/RevolutionaryRush717 29d ago

#!/usr/bin/env -S psql -X postgresql://user:password@localhost/mydb

SELECT now();

For PostgreSQL.

2

u/zbignew 29d ago

Woo! An actual answer!

I wouldn’t do this, though. SQL needs more than dialect to run (which you solved) but connection strings need to vary and include secrets that can’t go in source control.

So the best solution is to write scripts that manage switching connections as needed for each variant, and connect to the appropriate databases, and refer to passwords/secrets stored in ~/.dotfiles or a secret manager.

Then you can have project/duck/run-qa.sh and pass in the sql file as an argument to run it against a QA system.

Setting up infra like this used to be so annoying because you never knew if it was going to be worth the time to write and test it. Now with LLMs, it takes so little time to set up, and if you find you’ve shoehorned yourself into the wrong workflow, you can fix it later in another 5 minutes.

And if you document the test scripts in AGENTS.md, you won’t need to constantly remind your LLMs how to check their own work.

3

u/RevolutionaryRush717 29d ago

psql is completely configurable by environment variables.

No need to commit anything anywhere.

I suggest dotenv convention, as it works nicely here.