postgresql

How to query in jsonb postgresql and convert to predicate JPA

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 14:47:56
问题 i have stuck :) does anyone know how query postgresql in jsonb i have table USER INT id, Varchar name, jsonb categoryId the example data in categoryId field like this = [1,2,3,4,5] I have tried this query which works: select * from user where categoryId @> '2'::jsonb ; but how to query with multiple params like select * from user where categoryId @> '1,3,4'::jsonb and i will implement this to hibernate jpa/jpql-predicate, but i want to know native query first Thankyou so much 回答1: In SQL you

Creating a View while joining two tables in Alchemy

不打扰是莪最后的温柔 提交于 2021-02-11 14:46:30
问题 I m trying to join two tables and inserting the data in a view in PostgreSQL. While i debug I can see my data but when i check my DB there is no creating of a view. ALso the is nothing as error and i cannot find what's missing. engine = create_engine(DATABASE_URI) view = Table('testing', MetaData()) session = get_db_session() result=session.query(Policy_table,Join_table).filter(Policy_table.name==Join_table.policy_name).all() session.commit() create_view = CreateView(view ,result) engine

How to set DATABASE_URL environment variable for Postgres Mac terminal?

旧时模样 提交于 2021-02-11 14:45:19
问题 I am using postgres on Mac. I have a database up and running. I want to use it with Flask (SQL Alchemy) but the DATABASE_URL environment variable didn't create itself (I think it's supposed to do that). Here is the code I tried running: import os print(os.getenv("DATABASE_URL")) And there's no output. How can I set the variable (if I need to set it up manually). 回答1: Yes, you have to do that manually. So, within your terminal do this: % cd % nano .bash_profile It will open a file 'bash

Postgresql update a value in a jsonb array column

这一生的挚爱 提交于 2021-02-11 14:45:11
问题 I have one table with a jsonb and this data inside: [ { "valor": "2025,79", "parcela": 46, "vencimento": 1570503600000 }, { "valor": "1987,7", "parcela": 47, "vencimento": 1573182000000 }, { "valor": "1950,47", "parcela": 48, "vencimento": 1575774000000 }, { "valor": "1912,88", "parcela": 49, "vencimento": 1578452400000 } ] but now I need in all row, alter the value in "vencimento" from 1573182000000 to "10/10/2010" it's possible ? with this code I can split the array in columns and change te

TypeError: not all arguments converted during string formatting in psycopg2

十年热恋 提交于 2021-02-11 14:34:54
问题 When I run the below code with psycopg2: cur.execute( """INSERT INTO logmsg (msg_type, file, msg) VALUES %s;""", ["Error", str(file), str(sys.exc_info()[0])]) I get the following error: TypeError: not all arguments converted during string formatting Can someone help me with this? 回答1: VALUES needs a list of values enclosed in brackets: cur.execute( """INSERT INTO logmsg (msg_type, file, msg) VALUES (%s, %s, %s);""", ["Error", str(file), str(sys.exc_info()[0])]) Do not forget to commit the

Statement logging in PostgreSQL 12.1. Has something changed from earlier versions?

丶灬走出姿态 提交于 2021-02-11 14:34:20
问题 I'm debugging some code that uses SQLAlchemy to interact with a PostgreSQL database. The errors I'm seeing suggest that PostgreSQL is receiving my SQL statements, but perhaps not in the order I intended. So, I thought I'd enable statement logging to see what PostgreSQL is actually getting. I did that a few years ago, using the instructions in How to log PostgreSQL queries? and it worked just fine. (It must have, because I upvoted that question and the accepted answer.) However, that was

Writing to a PostgreSQL [CIDR] column with Spark JDBC

不羁岁月 提交于 2021-02-11 14:34:14
问题 I'm trying to write a Spark 2.4.4 dataframe to PostgreSQL via JDBC. I'm using Scala. batchDF. write. format("jdbc"). option("url", "jdbc:postgresql://..."). option("driver", "org.postgresql.Driver"). option("dbtable", "traffic_info"). option("user", "xxxx"). option("password", "xxxx"). mode(SaveMode.Append). save() One of the fields ( remote_prefix ) is of CIDR type in my table but is StringType in my dataframe, so I cannot write it as-is: ERROR: column "remote_prefix" is of type cidr but

Order SQL results where each record referencing another record on the same table comes after the referenced record

ⅰ亾dé卋堺 提交于 2021-02-11 14:27:19
问题 I have the following data: id, parent_id 1, 3 2, null 3, 2 4, 2 Where parent_id is a reference to the SAME table. I need to sort these columns so that each record is after its parent (not necessarily immediately after). So I would expect this result: id, parent_id 2, null 3, 2 1, 3 4, 2 I'm guessing there is no clean efficient way to do this without any significant schema changes, but in case anybody can think of a method I'm asking this here. One possible method would be to do multiple

docker-compose.yml, postgress, how to setup db, tables and prepop in an init.sql file?

人盡茶涼 提交于 2021-02-11 14:25:39
问题 I would like to add a DB, table and prepopulate with some kind of init.sql file. not sure what the init.sql file looks like. Also not sure where I would be putting the ADD command. This docker-compose.yml file works version: '3.6' services: postgre: image: postgres:11.1-alpine ports: - "5432" environment: POSTGRES_USER: 'user' POSTGRES_PASSWORD: 'password' POSTGRESS_DB: 'db_amex01' volumes: - ./init:/docker-entrypoint-initdb.d/ 回答1: You can add *.sh script(s) and/or *.sql instructions to the

Why is my Postgres database working for a while and then not able to “start server” once restarted?

余生长醉 提交于 2021-02-11 14:23:39
问题 Recently, I've started playing around with an old Raspberry Pi 3 b+, and I thought it would be good practice to host a Postgres database on my local network and use it for whatever I want to work through. I understand that running Postgres on a Raspberry Pi with 1GB of memory is not ideal and can take a toll on the SDcard, but I've updated the postgresql.conf file and specified that the data directory path is to utilize a 1TB SSD. Additionally, I've installed zram and log2ram to try and curb