简体   繁体   English

使用 crypt 创建一个带有密码的新列

[英]create a new column with passwords using crypt

I am currently trying to create a new column with passwords based on other column from the same table (tb_customer).我目前正在尝试使用基于同一表(tb_customer)中的其他列的密码创建一个新列。 The source to create the passwords is the column cust_cif.创建密码的来源是 cust_cif 列。

The script I used is as follows:我使用的脚本如下:

ALTER TABLE erp.tb_customer 
  ADD COLUMN password CHAR (34) USING crypt(erp.tb_customer.cust_cif, gen_salt('md5'));

I am getting an error and don't know why, since md5 doesn't need installation (but I did nevertheless).我收到一个错误,不知道为什么,因为 md5 不需要安装(但我还是安装了)。

The error is the following:错误如下:

ERROR:  does not exist type «crypt»
LINE 2:   ADD COLUMN password crypt(erp.tb_customer.cust_cif, gen_sa...

I also tried with我也试过

ALTER TABLE erp.tb_customer 
  ADD COLUMN password SELECT crypt(erp.tb_customer.cust_cif, gen_salt('md5'));

Here are some values shown in the table tb_customer :以下是表tb_customer中显示的一些值:

cust_no | cust_no | cust_name |客户名 | cust_cif | cust_cif | last_updated_by | last_updated_by | last_updated_date最后更新日期

"C0001" "PIENSOS MARTIN"    "A12345678" "SYSTEM"    "2022-04-28"
"C0002" "AGRICULTURA VIVES" "A66666666" "SYSTEM"    "2022-04-28"
"C0003" "CULTIVOS MARAVILLA"    "A55555555" "SYSTEM"    "2022-04-28"
"C0004" "ASOCIADOS PEREZ"   "A23126743" "SYSTEM"    "2022-04-28"
"C0005" "TECNICOS AVA"  "B34211233" "SYSTEM"    "2022-04-28"
"C0006" "AGR AGRI"  "B78788999" "SYSTEM"    "2022-04-28"
"C0007" "AGRIMARCOS"    "B98766562" "SYSTEM"    "2022-04-28"
"C0008" "CULTIVANDO ALEGRIA"    "B12333123" "SYSTEM"    "2022-04-28"
"C0009" "MARCOS LIMPIEZA"   "A87727711" "SYSTEM"    "2022-04-28"
"C0010" "VIAJES MUNDO"  "A00099982" "SYSTEM"    "2022-04-28"

What am I doing wrong?我究竟做错了什么?

Thank you in advance.先感谢您。

There is no ADD COLUMN...USING .没有ADD COLUMN...USING

You will have to add the column and populate it (UPDATE) in different statements.您必须在不同的语句中添加列并填充它(更新)。 Possibly in the same transaction.可能在同一个交易中。

I would do it this way:我会这样做:

ALTER TABLE erp.tb_customer 
ADD COLUMN password_cryp char(100);

--CREATE EXTENSION pgcrypto;

CREATE OR REPLACE PROCEDURE erp.col_cryp() AS $$
DECLARE
  _row record;
BEGIN
    FOR _row IN (
        SELECT cust_cif FROM erp.tb_customer
        )
    LOOP
        UPDATE erp.tb_customer
        SET password_cryp = crypt('cust_cif', gen_salt('md5'));
    END LOOP;
END;
$$LANGUAGE plpgsql;

CALL erp.col_cryp();

To test it you can try:要对其进行测试,您可以尝试:

SELECT ("password_cryp " = 'copy here the generated code on the table') AS IsMatch FROM erp.tb_customer;

Hope it works!希望它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM