在postgresql中的insert语句中加密密码会引发错误(需要添加显式类型转换) [英] Encrypt password in insert statement in postgresql throws an error (Need to add explicit type cast)

查看:995
本文介绍了在postgresql中的insert语句中加密密码会引发错误(需要添加显式类型转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的postgresql。这可能很简单但是没有得到它。



我在mysql中有这个:

  insert into APP_USERS VALUES 
(1,'admin','adminemailid','','System','Administrator','SysAdmin','CompanyName',
sha('password123' ,'ADMIN',2,'SCREEN',0);

在引用Postgresql文档后,对Stackoverflow进行密码加密/散列和各种类似的问题;



我在postgresql中尝试过等同于:

 插入APP_USERS VALUES 
(1,'admin','adminemailid','','System','Administrator','SysAdmin','CompanyName',
crypt('password123',gen_salt('sha1')),'ADMIN ',2,'SCREEN',0)

但是它在函数 gen_salt()中抛出一个错误:



没有函数匹配给定的名称和参数类型。您可能需要添加明确的类型转换。



(列(字段)密码的数据类型为字符变化。 / p>

我在postgresql中阅读了有关类型转换的各种手册,并尝试转换函数。



但是;不用我遇到了各种类似的错误。



我可以知道究竟是什么吗?或者铸造的确切方式是什么?
或者我需要添加chkpass模块(扩展名)吗?



我已经有pgcrypto扩展名,并且使用postgresql 9.6。

解决方案

实际上不是答案,而是简要说明如何找到错误的来源。



首先,我们可以找到 pgcrypto 扩展名:

  select e.extname,n.nspname 
from pg_catalog.pg_extension e left join pg_catalog.pg_namespace n on n.oid = e.extnamespace;

应该返回以下内容:

 ╔═══════════════════════════════$ $ $ $║║name │nspname║
╠═════════════════════════════════════════════╣$║║║║│││││││││││││││││││││ ║
║adminpack│pg_catalog║
║plpythonu│pg_catalog║
║pg_stat_statements│public║
║plpython3u│pg_catalog║
║hstore│public║
║uuid-ossp│public║
║pg_trgm│public║
║ltree│public║
║tablefunc│public║
║noncent│public║
║pgcrypto│ nd║
╚══════════════════════════════════════════ ════════╝

所以 pgcrypto 安装在我的数据库中的模式 nd 中。



接下来让我们来看看 search_path 参数,提供搜索DB对象的信息:

  show search_path; 
╔════════════════╗
║search_path║
╠═══════════════ ══╣
║$ user,public║
╚═══════════════╝

这意味着将在模式中搜索对象,名称为当前用户,如果没有找到对象,则在模式 public



最后可以检查当前用户:

  select current_user; 
╔═════════════╗
║current_user║
╠═════════════
║postgres║
╚═════════════╝

因此,您可以看到在 nd 中安装的扩展程序无法使用 search_path postgres,public ,我们收到错误:

  select gen_salt( 'MD5'); 
错误:函数gen_salt(未知)不存在
LINE 1:select gen_salt('md5');
^
提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。

根据上述DB的实际值,有几种方法可以修复它。 p>

您可以将扩展名移动到另一个模式,如:

  alter extension pgcrypto设置模式public; 

您可以更改 search_path 选项,如:

 将search_path设置为''$ user,public'; - 仅活动会话
alter database your_db_name将search_path设置为'$ user',public'; - 永久为数据库

最后,您可以在SQL语句中显式指定模式: p>

  select schema_where_it_installed.gen_salt('md5'); 
- 对于我的例子:
select nd.gen_salt('md5');


New to postgresql. It might be very simple; but am not getting it.

I've this in mysql:

insert into APP_USERS VALUES
(1, 'admin', 'adminemailid','', 'System', 'Administrator', 'SysAdmin', 'CompanyName',
sha('password123'), 'ADMIN', 2,'SCREEN',0);

After referring postgresql documentations on password encryption/hashing and various similar questions on Stackoverflow;

I tried it's equivalent in postgresql as:

insert into APP_USERS VALUES
(1, 'admin', 'adminemailid','', 'System', 'Administrator', 'SysAdmin', 'CompanyName',
crypt('password123',gen_salt('sha1')), 'ADMIN', 2,'SCREEN',0)

But; it throws me an error at function gen_salt():

No function matches the given name and argument types. You might need to add explicit type casts.

(The datatype of column(field) password is character varying.)

I read various manuals on type casting in postgresql and tried casting the function.

But; no use. I came across various similar errors.

Can I know what exactly am missing? or what is the exact way of type casting? Or do I need to add chkpass module (extension) for this?

I already have pgcrypto extension and am using postgresql 9.6.

解决方案

It is not the answer actually but a brief explanation how to find the source of the error.

First of all lets find the pgcrypto extension:

select e.extname, n.nspname
from pg_catalog.pg_extension e left join pg_catalog.pg_namespace n on n.oid = e.extnamespace;

It should return something like:

╔════════════════════╤════════════╗
║      extname       │  nspname   ║
╠════════════════════╪════════════╣
║ plpgsql            │ pg_catalog ║
║ adminpack          │ pg_catalog ║
║ plpythonu          │ pg_catalog ║
║ pg_stat_statements │ public     ║
║ plpython3u         │ pg_catalog ║
║ hstore             │ public     ║
║ uuid-ossp          │ public     ║
║ pg_trgm            │ public     ║
║ ltree              │ public     ║
║ tablefunc          │ public     ║
║ unaccent           │ public     ║
║ pgcrypto           │ nd         ║
╚════════════════════╧════════════╝

So the pgcrypto installed in the schema nd in the my database.

Next lets look to the search_path parameter which provide the information where to search DB objects:

show search_path;
╔═════════════════╗
║   search_path   ║
╠═════════════════╣
║ "$user", public ║
╚═════════════════╝

It means that the objects will be searched in the schema with name of the current user and, if nothing found, in the schema public.

Finally lets check the current user:

select current_user;
╔══════════════╗
║ current_user ║
╠══════════════╣
║ postgres     ║
╚══════════════╝

So as you can see the extension installed in the schema nd can not be found using the search_path which actually is postgres, public and we got the error:

select gen_salt('md5');
ERROR:  function gen_salt(unknown) does not exist
LINE 1: select gen_salt('md5');
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

There are several ways how to fix it depending of the actual values for your DB mentioned above.

You can move the extension to another schema like:

alter extension pgcrypto set schema public;

You can change the search_path option like:

set search_path to '"$user", public'; -- for the active session only
alter database your_db_name set search_path to '"$user", public'; -- permanently for the database

Finally you could to explicitly specify the schema in the SQL statement:

select schema_where_it_installed.gen_salt('md5');
-- For my example:
select nd.gen_salt('md5');

这篇关于在postgresql中的insert语句中加密密码会引发错误(需要添加显式类型转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆