postgresql

GORM create record that might already exist

▼魔方 西西 提交于 2021-01-03 05:30:06
问题 I'm using gorm with postgres in my Go app. I want to create a new user in the database, but there is a good chance that that user will already exist. If so, I want to not do anything with the database, but I want know about it so I can tell the user. The good news is, that's already what gorm.Create(..) does. Trying to create a record with a duplicate unique key will return an error. There are two problems: I want better error messages. I want to write custom user-facing error messages that

Returning set of rows from plpgsql function.

自古美人都是妖i 提交于 2021-01-02 08:10:13
问题 I would like to return table from plpgsql function. Here is my code. CREATE FUNCTION test() RETURNS my_table AS $BODY$DECLARE q4 my_table; BEGIN q4 := SELECT * FROM my_table; RETURN q4; END;$BODY$ LANGUAGE sql; I am getting following error: Error: ERROR: syntax error at or near "SELECT" LINE 5: q4 := SELECT * FROM my_table; I started from this questions/tutorials. https://dba.stackexchange.com/questions/35721/declare-variable-of-table-type-in-pl-pgsql && http://postgres.cz/wiki/PL/pgSQL_%28en

Returning set of rows from plpgsql function.

寵の児 提交于 2021-01-02 08:09:57
问题 I would like to return table from plpgsql function. Here is my code. CREATE FUNCTION test() RETURNS my_table AS $BODY$DECLARE q4 my_table; BEGIN q4 := SELECT * FROM my_table; RETURN q4; END;$BODY$ LANGUAGE sql; I am getting following error: Error: ERROR: syntax error at or near "SELECT" LINE 5: q4 := SELECT * FROM my_table; I started from this questions/tutorials. https://dba.stackexchange.com/questions/35721/declare-variable-of-table-type-in-pl-pgsql && http://postgres.cz/wiki/PL/pgSQL_%28en

could not determine data type of parameter $1 in python-pgsql

孤人 提交于 2021-01-02 08:02:08
问题 I have a simple table (named test) as: id | integer name | character varying(100) intval | integer When I try to use prepare statement to update the name like this in python. (I am using python-pgsql http://pypi.python.org/pypi/python-pgsql/) >>> for i in db.execute("select * from test"): print i ... (1, 'FOO', None) >>> query = "UPDATE test set name = '$1' where name = '$2'" >>> cu.execute(query, "myname", "FOO") Traceback (most recent call last): File "<input>", line 1, in <module> File "

How to store JSON object into PostgreSQL using JSONB data type inside table and PostgreSQL JDBC driver

喜夏-厌秋 提交于 2021-01-02 06:46:06
问题 I want to save following json object into PostgreSQL db table as jsonb { "fname":"john", "lname:"doe", } I am currenlty using PGObject to create object and set type to jsonb and pass value as json string Looking for a better approach with micronaut-data and micronaut Is there any native data type supported in micronaut-data to convert the Java object to JSON and store in db? How to save the data using postgres jdbc driver typecasting in query using :jsonb is already tried with raw jdbc if it

Convert IP address in PostgreSQL to integer?

喜夏-厌秋 提交于 2021-01-02 05:58:27
问题 Is there a query that would be able to accomplish this? For example given an entry '216.55.82.34' ..I would want to split the string by the '.'s, and apply the equation: IP Number = 16777216*w + 65536*x + 256*y + z where IP Address = w.x.y.z Would this be possible from just a Query? 回答1: You can use split_part() . For example: CREATE FUNCTION ip2int(text) RETURNS bigint AS $$ SELECT split_part($1,'.',1)::bigint*16777216 + split_part($1,'.',2)::bigint*65536 + split_part($1,'.',3)::bigint*256 +

Spring data repository sends null as bytea to PostgreSQL database

守給你的承諾、 提交于 2021-01-02 05:11:25
问题 After switching from MySQL to PostgreSQL I found out that my SQL query (@Query in spring data repository interface) does not work anymore. The issue is caused by null value being sent as bytea and I'm getting following exception: Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Repository with @Query: public interface WineRepository extends

【ABP.Net】2.多数据库支持&&初始化数据库

亡梦爱人 提交于 2021-01-02 05:09:20
abp默认连接的数据库是MSSQL,但是在开发过程中往往很多开发者不满足于mssql。 所以这里演示一下把mssql改成postgresql,来进行接下来的系统开发。 abp的orm是用EF的。那么我们就从ef动手。 首先我们先安装下面两个库 Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -Version 2.2 Install-Package Npgsql.EntityFrameworkCore.PostgreSQL.Design -Version 1.1.1 顺带一提,如果想用mysql的话 Install-Package MySql.Data.EntityFrameworkCore -Version 8.0.15 安装完成之后,我们只要修改一下配置文件 把builder.UseSqlServer修改成UseNpgsql 然后修改Migrator层的根目录添加配置文件appsettings.json的连接字符串 下面是内容 { " ConnectionStrings " : { " Default " : " " //写入你的连接字符串 } } 将启动项设置成 运行。 键入Y 然后数据库就初始完毕了。期间报了一个错误,原因是创建初始化数据的时候,不为空的字段变成了空的。 我的解决方案是用ef

How to compare 2 successive row values in a resultset object using python

帅比萌擦擦* 提交于 2021-01-02 04:11:25
问题 I have a table issue_logs : id | issue_id | from_status | to_status | up_date | remarks ----+----------+-------------+-----------+----------------------------------+----------- 29 | 20 | 10 | 11 | 2018-09-14 11:43:13.907052+05:30 | UPDATED 28 | 20 | 9 | 10 | 2018-09-14 11:42:59.612728+05:30 | UPDATED 27 | 20 | | 9 | 2018-09-11 17:45:35.13891+05:30 | NEW issue 26 | 19 | 9 | 11 | 2018-09-06 16:37:05.935588+05:30 | UPDATED 25 | 19 | | 9 | 2018-09-06 16:27:40.543001+05:30 | NEW issue 24 | 18 | 11

How to specify Schema in psycopg2 connection method?

十年热恋 提交于 2021-01-01 07:29:20
问题 Using the psycopg2 module to connect to the PostgreSQL database using python. I'm able to execute all of my queries using the below connection method. Now I want to specify a different schema other than the public to execute my SQL statements. Is there any way to specify the schema name in the connection method? conn = psycopg2.connect(host="localhost", port="5432", user="postgres", password="password", database="database", ) I tried to specify schema directly inside the method. schema=