Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created March 27, 2024 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHERTS/af1ce5338f837af5667e6a4ff60a4596 to your computer and use it in GitHub Desktop.
Save CHERTS/af1ce5338f837af5667e6a4ff60a4596 to your computer and use it in GitHub Desktop.
CREATE DATABASE NOT EXISTS in PostgreSQL (workaround from within psql)
# native using psql
SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec
# native using shell + psql
echo "SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec" | psql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment