postgresql

Psycopg2 can not create table

北战南征 提交于 2021-01-29 19:36:49
问题 From Jupiter notebook, I was able to create Database with Psycopg2. But somehow I was not able to create Table and store element in it. import psycopg2 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT con = psycopg2.connect("user=postgres password='abc'"); con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT); cursor = con.cursor(); name_Database = "socialmedia"; sqlCreateDatabase = "create database "+name_Database+";" cursor.execute(sqlCreateDatabase); With the above code, I can see

How to change time zome in different tables in Laravel

时光总嘲笑我的痴心妄想 提交于 2021-01-29 19:34:34
问题 Can you give an idea? I have two tables in to postgresql DB. The are filled with recods by two separate csv files. But the records of the first csv file are with local timestamp. The second csv file is with timestamps UTC. So I need to change the time in local of the view in this table that is filled with records with timezone UTC. I used to deal with that problem before using laravel with this code in every page that I need to do that, for example: date_default_timezone_set('Europe/Sofia');

Filter parent by it child ids

与世无争的帅哥 提交于 2021-01-29 19:12:55
问题 I have two tables, which look like this (simplified): Operations: |operation_id|description|created_at| |------------|-----------|----------| | 1| abc|2020-04-04| Transactions |transaction_id|amount|created_at|user_id|operation_id|type| |--------------|------|----------|-------|------------|----| | 1| 400|2020-04-04| 1| 1| tr| | 2| 400|2020-04-04| 2| 1| tr| How can I find all operations in which transaction.user_id 's intersects with an array of user ids? E.g. for array [1, 2] there will be

Merge in postgres

与世无争的帅哥 提交于 2021-01-29 19:07:04
问题 Am trying to convert below oracle query to postgres, MERGE INTO table1 g USING (SELECT distinct g.CDD , d.SGR from table2 g, table3 d where g.IDF = d.IDF) f ON (g.SGR = f.SGR and g.CDD = f.CDD) WHEN NOT MATCHED THEN INSERT (SGR, CDD) VALUES (f.SGR, f.CDD); I made changes as below compatible to postgres: WITH f AS ( SELECT distinct g.CDD , d.SGR from table2 g, table3 d where g.IDF = d.IDF ), upd AS ( update table1 g set SGR = f.SGR , CDD = f.CDD FROM f where g.SGR = f.SGR and g.CDD = f.CDD

How to perform date comparisons against postgres with sequelize

爱⌒轻易说出口 提交于 2021-01-29 18:51:16
问题 I want to delete all records with dates before 20 minutes ago. Postgres (or Sequelize) is not satisfied with the bare javascript Date object I provide as the comparison value. I'm using sequelize 4.37 on top of a postgres 9.6 database. The column in question was declared with type: Sequelize.DATE , which research suggests is equivalent to TIMESTAMP WITH TIME ZONE : a full date and time with microsecond precision and a timezone signifier. (That is also what I see when I use the psql CLI tool

How can I improve error handling on a pool

孤街醉人 提交于 2021-01-29 18:45:37
问题 I am have created a class User that will hold the logic for inserting a new user into a postresql database. My code works perfectly but i think it is poorly written and would like some views on how to improve it, especially error handling. const pool = require('../config/config.js'); // user constructor class User { constructor(user) { this.username = user.username; this.email = user.email; this.password = user.password; this.role = user.role; } // save new user in databes createUser(res) {

Indexing Postgresql JSONB arrays for element existence and unicity

与世无争的帅哥 提交于 2021-01-29 18:37:12
问题 I have a Postgresql 11.8 table named posts where I would like to define a column slugs of type JSONB, which would contain arrays of strings such as ["my-first-post", "another-slug-for-my-first-post"] . I can find a post having a specific slug using the ? existence operator: SELECT * FROM posts WHERE slugs ? 'some-slug' . Each post is expected to only have a handful of slugs but the amount of posts is expected to grow. Considering the above query where some-slug could be any string: How can I

SQL database relationships: unable to determine between manyToMany and oneToMany

Deadly 提交于 2021-01-29 18:34:48
问题 I have two database tables "Users" and "Transactions" where the "Transactions" table looks like below. The "Transactions" table has a record of each transaction between two accounts where the differentiator is "senderAccount" and "recipientAccount" columns. id | transactionId | senderAccount | recipientAccount | Amount | —--+---------------+---------------+------------------+--------+ 1 | ijiej33 | A | B | 100 | The relationship between "Users" and "Transactions" is one to many since every

Postgres COPY two files to same table with source indication?

末鹿安然 提交于 2021-01-29 18:23:57
问题 I'm using Postgres 12. I have 2 files that I want to load into a table via COPY. However, I need the rows from each file to indicate their source. Is there some trick with COPY where I could specify a default value for a column? I can't use the DEFAULT mechanism on the table declaration itself since that'd be universal, not different per file. The only hack I've come up with so far is running the files through a pre-step to add the column to the raw file, but that's painful as I'm trying to

How to view results as soon as they arrive in psql?

旧时模样 提交于 2021-01-29 18:10:10
问题 When executing a SQL query on a Postgres database using the psql client, it nicely formats the results and displays them in a pager (by default). How to change this behavior to make psql display results as soon as they are available rather than waiting for the full result set to computed (in a larger computation)? Of course, aligned output cannot be expected in this case, because the width of the individual columns is not known before they are to be printed. 回答1: Not processing the full