gpt4 book ai didi

How to create tables in posgresql in helm(如何在helm中使用posgresql创建表)

转载 作者:bug小助手 更新时间:2023-10-22 13:50:24 38 4
gpt4 key购买 nike



I have deployed postgresql instance in k8s using helm and creating a database using values.yaml. Now How to create table.

我已经使用helm在k8s中部署了postgresql实例,并使用values.yaml创建了一个数据库。现在如何创建表。


global:
postgresql:
auth:
# Added below credentials for testing
postgresPassword: "postgresqladmin"
username: "admin"
password: "admin"
database: "postgresqlDB"
containerPorts:
postgresql: 5432
image:
registry: docker.io
repository: bitnami/postgresql
tag: 15.4.0-debian-11-r10
digest: ""
pullPolicy: IfNotPresent
debug: false

Here is the command to install:

以下是要安装的命令:


helm upgrade --install postgresql -f values.yaml bitnami/postgresql

And here is the command to access db(I am using suggested command after install)

这是访问数据库的命令(我在安装后使用建议的命令)


kubectl run postgresql-client --rm --tty -i --restart='Never' --image bitnami/postgresql --env="admin" --command -- psql --host postgresql -U admin -d postgresqlDB -p 5432

更多回答

You can create a tunnel and access the db-server using psql.

您可以创建一个隧道并使用psql访问数据库服务器。

how to do that?

怎么做?

Pasted from the console when installing postgresql: kubectl port-forward --namespace default svc/db-server 5432:5432 & PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432

安装postgresql时从控制台粘贴:kubectl port forward--命名空间默认svc/db服务器5432:5432 PGPASSWORD=“$POSTGRES_PASSWORD”psql--主机127.0.0.1-U POSTGRES-d POSTGRES-p 5432

I have updated question with command i use to connect to database

我用连接数据库的命令更新了问题

If you need help creating tables in postgresql you are better off looking at tutorials on this topic. This can get you started. docs.bitnami.com/virtual-machine/infrastructure/postgresql/…

如果您需要在postgresql中创建表的帮助,最好查看有关此主题的教程。这可以让你开始。docs.bitnami.com/virtualmachine/instructure/postgresql/…

优秀答案推荐

You have two options.

你有两个选择。


1. Access container

1.接入容器


You can access to your database after it has been mounted like this, and then execute your sql script.

您可以在这样安装数据库后访问数据库,然后执行sql脚本。


kubectl run -it --rm --image=bitnami/postgresql:15.4.0-debian-11-r10 --restart=Never postgresql-client -- psql -h postgresql -U admin -d postgresqlDB

kubectl run-it--rm--image=bitnami/postgresql:115.4.0-debian-11-r10--restart=Never postgresql client--psql-h postgresql-U admin-d postgresqlDB


2. Include a script to your deployment :

2.在部署中包含脚本:


You create an SQL script that create your table myscript.sql :

您可以创建一个SQL脚本来创建表myscript.SQL:


CREATE TABLE my_table (
id serial PRIMARY KEY,
field VARCHAR(255)
);

Create a ConfigMap, a YAML file (e.g., configmap-create-my-table.yaml) with the following content:

创建一个ConfigMap,一个具有以下内容的YAML文件(例如,ConfigMap Create my table.YAML):


apiVersion: v1
kind: ConfigMap
metadata:
name: create-my-table-configmap
data:
create_table.sql: |
-- create_table.sql
CREATE TABLE my_table (
id serial PRIMARY KEY,
field VARCHAR(255)
);

Apply this to your cluster
kubectl apply -f configmap-create-table.yaml

将其应用于集群kubectl Apply-f configmap-create-table.yaml


Adapt your helm chart :

调整舵表:


postgresql:
extraVolumeMounts:
- name: create-my-table-configmap
mountPath: /docker-entrypoint-initdb.d
readOnly: true
extraVolumes:
- name: create-my-table-configmap
configMap:
name: create-my-table-configmap

And then upgrade it :

然后升级:


helm upgrade --install postgresql -f values.yaml bitnami/postgresql

helm升级--安装postgresql-f values.yaml-bitnami/postgresql



In order to create table after you have created database using values.yaml, try these steps;

为了在使用values.yaml创建数据库后创建表,请尝试以下步骤;


First you have to connect to postgreSQL using client:

首先,您必须使用客户端连接到postgreSQL:


kubectl run -it --rm --image=bitnami/postgresql:15.4.0-debian-11-r10 --namespace=YOUR_NAMESPACE postgresql-client -- bash

Now connect to your database inside the postgreSQL client pod;

现在连接到postgreSQL客户端pod中的数据库;


psql -h postgresql -U admin -d postgresqlDB

NOw using simple SQL Commands, you can create tables;

NOw使用简单的SQL命令,可以创建表;


CREATE TABLE my_table (
id serial PRIMARY KEY,
name VARCHAR (255),
age INT
);

After successfully creating the tables, you can exit the PostgreSQL client using;
ostgreSQL

成功创建表后,您可以使用退出PostgreSQL客户端;ostgreSQL


\q

Hope it works:)

希望它能起作用:)



once you start the database using -

使用启动数据库后-


kubectl run postgresql-client --rm --tty -i --restart='Never' --image bitnami/postgresql -- bash

You'd need to connect to the instance via -

您需要通过连接到实例-


psql -h postgresql -U <your-username> -d <your-database>

You will be prompted for your password, which you specified in your values.yaml file (in your case, it's "postgresqladmin").

系统将提示您输入密码,该密码是您在values.yaml文件中指定的(在您的情况下,它是“postgresqladmin”)。


After this, you'll see the cursor change to postgresql / the username you specified, this indicates that you can now access the database using query language.

之后,您将看到光标更改为postgresql/您指定的用户名,这表明您现在可以使用查询语言访问数据库。


To create a table from here on, you can simply type in

要从此处创建表,只需键入


CREATE TABLE example (
col1 serial PRIMARY KEY,
col2 INT,
col3 BOOLEAN
);

and it will create these columns with the specified data types and the primary key. You can exit the client using \q and exit the container futher using exit.

它将使用指定的数据类型和主键创建这些列。您可以使用\q退出客户端,然后使用exit退出容器。


更多回答

Thank you for an answer This looks very clear and easy to use, But i am not using config map at all I have only the above commands to install. Is it necessary to have config-map?

谢谢你的回答这看起来很清楚,也很容易使用,但我根本没有使用配置映射,我只有上面的命令要安装。有必要配置映射吗?

This is not the only option, you can also use Docker init by using a custom image like this : ``` FROM postgres:15.4 # Copy your initialization scripts into the container COPY init-scripts/* /docker-entrypoint-initdb.d/ ``` But I think you can also use initContainer in your Kubernetes Pod specification to perform initialization tasks before the main PostgreSQL container starts.

这不是唯一的选项,您还可以使用Docker init,使用如下自定义映像:``FROM postgres:15.4#将初始化脚本复制到容器Copy init scripts/*/Docker-entrypoint-initdb.d/``但我认为您也可以在Kubernetes Pod规范中使用initContainer在PostgreSQL主容器启动之前执行初始化任务。

I think using docker wil be a good option for me. Do you have any example? or do you know if i can just run sql script file in kubectl run command?

我认为使用docker对我来说是个不错的选择。你有什么例子吗?或者你知道我是否可以在kubectl run命令中运行sql脚本文件吗?

You should mount volume /myscripts/ : /docker-entrypoint-initdb.d Those scripts will be executed by Docker. In a docker-compose it would look like this : ``` volumes: - ./myScriptsFolder/:/docker-entrypoint-initdb.d ```

您应该挂载volume/myscripts/:/docker entrypoint initdb.d这些脚本将由docker执行。在docker compose中,它看起来是这样的:``volumes:-/myScriptsFolder/:/docker入口点initdb.d```

How to use my sql script file(create.sql) instead of typing script

如何使用我的sql脚本文件(create.sql)而不是键入脚本

for that, you need to copy the script file into your kube, using kubectl cp create.sql postgresql-client:/create.sql and then psql -h postgresql -U <your-username> -d <your-database> -f /create.sql

为此,您需要将脚本文件复制到您的kube中,使用kubectl cp create.sql postgresql client:/create.sql,然后使用psql-h postgresql-U<您的用户名>-d<您的数据库>-f/create.sql

how to run these commands sequentially kubectl run postgresql-client --rm --tty -i --restart='Never' --image bitnami/postgresql -- bash kubectl cp ./create.sql postgresql-client:/create.sql psql -h postgresql -U <your-username> -d <your-database> -f /create.sql

如何依次运行这些命令kubectl run postgresql client--rm--tty-i--restart='Never'--image bitnami/postgresql--bash kubectl cp/create.sql postgresql客户端:/create.sql psql-h postgresql-U<您的用户名>-d<您的数据库>-f/create.sql

cannot exec into a container in a completed pod; current phase is Failed. I get this error when i try to copy script file

无法执行到已完成pod中的容器中;当前阶段为失败。尝试复制脚本文件时出现此错误

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com