我在后端使用Go语言。我试图编写一个函数,该函数接受数据库名称并使用该名称创建PostgreSQL数据库。然后,该函数应在此数据库中创建表格(我已经为此任务创建了SQL脚本)。
因此,主要问题是我不知道如何编写一个可以创建PostgreSQL数据库的函数。我想过创建一个 .sql 文件,并以某种方式将数据库名称传递给该文件(例如,在 .sql 文件中查找类似于 {{dbname}} 的字符串,并用实际的数据库名称替换它),但也许有更好的方法?
这是我的 .sql 文件,它应该在新的PostgreSQL数据库中创建表格。
因此,主要问题是我不知道如何编写一个可以创建PostgreSQL数据库的函数。我想过创建一个 .sql 文件,并以某种方式将数据库名称传递给该文件(例如,在 .sql 文件中查找类似于 {{dbname}} 的字符串,并用实际的数据库名称替换它),但也许有更好的方法?
这是我的 .sql 文件,它应该在新的PostgreSQL数据库中创建表格。
create table "reviews"
(
review_id uuid not null
constraint review_pk
primary key,
user_id uuid not null,
rating float,
comment text,
date date not null
);
create unique index reviews_review_id_uindex
on "reviews" (review_id);
create unique index reviews_user_id_uindex
on "reviews" (user_id);
create table "sections"
(
section_id uuid not null
constraint section_pk
primary key,
title text not null,
color text,
created_at date not null,
updated_at date not null,
deleted boolean default false
);
create unique index sections_section_id_uindex
on "sections" (section_id);