zer*_*ing -1 postgresql kubernetes
我有一个在 kubernetes 上运行的 postgres 集群,该集群是由https://github.com/zalando/postgres-operator创建的。在CD上的部署过程中,我想执行一些语句,例如创建、插入等,为服务准备数据库。
sql 文件可能包含以下语句:
CREATE TABLE public.genders
(
id "char" NOT NULL,
text character varying(20) NOT NULL,
PRIMARY KEY (id)
);
ALTER TABLE public.genders
OWNER to dbuser;
CREATE TABLE public.interests
(
id smallserial NOT NULL,
interest character varying(40) NOT NULL,
PRIMARY KEY (id)
);
ALTER TABLE public.interests
OWNER to dbuser;
Run Code Online (Sandbox Code Playgroud)
问题是,如何将sql文件应用到postgres集群上呢?我需要 psql 客户端吗?
是的你是对的。您必须在本地或容器中安装 psql 客户端。
第一种方法:在主机中安装 psql 客户端,您可以使用 kubectl 命令访问 kube api。然后转发你的数据库服务端口
kubectl -n pgo port-forward svc/postgresdb-servicename 5432:5432
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令连接并运行查询
psql -h localhost -U postgres postgres(db name)
Run Code Online (Sandbox Code Playgroud)
第二种方法:您可以使用 docker image 执行相同的操作,例如
kubectl run -it --rm --image=postgres:12-alpine psql bash
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令连接并运行查询
psql -h localhost -U postgres postgres(db name)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1446 次 |
| 最近记录: |