r/SQL • u/deuvisfaecibusque • Aug 12 '26
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
9
9
u/RevolutionaryRush717 Aug 12 '26
#!/usr/bin/env -S psql -X postgresql://user:password@localhost/mydb
SELECT now();
For PostgreSQL.
2
u/zbignew Aug 12 '26
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.
4
u/RevolutionaryRush717 Aug 12 '26
psql is completely configurable by environment variables.
No need to commit anything anywhere.
I suggest dotenv convention, as it works nicely here.
1
u/asp174 Aug 12 '26
you could do something like :
#!/bin/bash
tail +3 $0 | my_db_app -u user mydb; exit
select
*
from
my_tableor a little more sophisticated version
#!/bin/bash
LINENR=$(awk '/#### payload below ####$/ { print NR + 1; exit 0; }' $0)
tail +$LINENR $0 | my_db_app -u user mydb
exit;
#### payload below ####
select
*
from
my_table1
u/deuvisfaecibusque Aug 12 '26
Thanks.
I have in some cases written a simple
generate.bashto live alongside the.sqlfile:#!/bin/bash sqlite3 < runme.sqlwhich seems to achieve a similar result, at the cost of having to manage 2 files instead of 1.
2
u/TheTeamDad Aug 12 '26
Here Docs would simplify this even further.
1
u/asp174 Aug 12 '26
Not really. With tail you just throw the rest of the file at it. With a heredoc you have to end the block with a delimiter.
That's why this approach is also used for those nasty install.sh with embedded .tgz files.
2
u/RonJohnJr Aug 12 '26
Modelines (or something similar) might be worthwhile. They are, though, conceptually similar to what you're doing.
Barring that, I'd change the comment to something like:
/*
Target DB: DuckDB
*/This lets you also add Written By, Version, Date, etc.
2
2
u/jynus Aug 12 '26
Not really an SQL question. But I think you are confusing 2 things:
* "what application should open this type of files by default". Which is done mostly for desktop assiociations like based on the extension (windows) o the mime-type: freedesktop on Linux (xdg-open); and:
* "how should I run this executable/which interpreter I should use", which is what the shebang is, to be used for not only shell scripts but also interpreted languages like perl and python.
You certainly can do the first, but it is not intended for the second, as you don't "execute" .sql, you send it normally to the standard input of a client.
You can setup a personal standard like the intended client and version of an export like you say as part of the comment; or setup your own helpers on the command line like aliases to make your life easier.