postgresql - sql文件中的Postgresql命令行变量使用
问题描述
我正在使用 psql 命令将文件绕过参数调用为-v argument=value
. 在 SQL 文件中,参数需要用作字符串的一部分。例如prefix_:argument_suffix
. 这是行不通的。如果我按照prefix_:argument
它的工作原理使用它,但是当涉及到后缀时它不会。
create database
这是为了在语句中建立一个数据库名称。
有人可以帮忙吗?
解决方案
使用另一个变量,例如:
nd@postgres=# \set argument 'aaa'
nd@postgres=# \set dbname 'foo_':argument'_bar'
nd@postgres=# select :'dbname';
┌─────────────┐
│ ?column? │
├─────────────┤
│ foo_aaa_bar │
└─────────────┘
nd@postgres=# create database :dbname;
CREATE DATABASE
nd@postgres=# \l
List of databases
┌─────────────┬──────────┬──────────┬─────────────┬─────────────┬───────────────────────┐
│ Name │ Owner │ Encoding │ Collate │ Ctype │ Access privileges │
├─────────────┼──────────┼──────────┼─────────────┼─────────────┼───────────────────────┤
│ foo_aaa_bar │ nd │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ │
│ postgres │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ │
│ template0 │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ =c/postgres ↵│
│ │ │ │ │ │ postgres=CTc/postgres │
│ template1 │ postgres │ UTF8 │ en_US.UTF-8 │ en_US.UTF-8 │ =c/postgres ↵│
│ │ │ │ │ │ postgres=CTc/postgres │
└─────────────┴──────────┴──────────┴─────────────┴─────────────┴───────────────────────┘
推荐阅读
- r - 在 data.table 的特定列中将值设置为零
- javascript - 如何让 ReactRouter 忽略一个 URL?
- javascript - 通过 ID javascript 合并数组中的对象
- python - Python 解码 NDFD Grib 二进制文件,类似于使用 Open(path, 'wb') 和 xarray.load_dataset(path, engine='cfgrib') 写入然后读取文件
- python-3.x - 加载以前保存的 TensorFlow 模型错误
- go - 通过错误检查缩短重复代码
- python - 如何在烧瓶中进行 LDAP 身份验证
- sql - 如何按在 SQL 中购买的商品对客户姓名进行排序
- python - 从叠加图像中提取多边形
- pandas - Pandas 条形图比较两个不同年份的天数