postgresql

set “VALID UNTIL” value with a calculated timestamp

℡╲_俬逩灬. 提交于 2021-01-27 21:30:58
问题 I want to create user or change password with a timestamp calculated over current time. example: # CREATE USER user WITH PASSWORD 'password12345678' VALID UNTIL '(NOW() + interval 1 month)'; of course it's not valid: ERROR: invalid input syntax for type timestamp with time zone: "(NOW() + interval 1 month)" 回答1: Utility commands like CREATE USER do not accept expressions, only literals. You need dynamic SQL. DO $do$ BEGIN EXECUTE format($$CREATE USER myuser WITH PASSWORD 'password12345678'

liquibase gradle postgresql wrong driver

吃可爱长大的小学妹 提交于 2021-01-27 21:24:36
问题 Environment: Spring boot (STS 3.9.1) Gradle 2.13 Postgresql 9.6.5 Liquibase Gradle plugin (liquibase-gradle-plugin:1.2.4) Scenario: We have a spring boot rest api, we want it to have a CI process that updates the database at a stage of the process, for this we want to use gradle for it buildscript { repositories { mavenCentral() } dependencies { classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE') // tag::build[] classpath('se.transmode.gradle:gradle-docker:1.2') //

Insert data into Postgresql with duplicate values

和自甴很熟 提交于 2021-01-27 21:21:34
问题 I need to insert dataset in postgresql. INSERT INTO table_subject_topics_exams (name_of_subject, section, topic, subtopic) VALUES ('Algebra', 'Mathematics', 'Progressions', 'Number Sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions', 'Number sequences'), ('Algebra', 'Mathematics', 'Progressions',

Database design - nullable fields

倖福魔咒の 提交于 2021-01-27 21:05:57
问题 Bit of a 'best practice' question as I am new to DB design and I wanted to make sure I am on the right tracks with this one I have 3 user types, user (single person), group (lots of users) and company (lots of groups), each has their own login which allows them to post messages. So eg. if a company posts a message it will appear in all the linked users news feeds. To achieve this I have a table 'messages' that stores the message contents, along with the foreign keys to link the user types I

In Postgresql, how to select top n percent of rows by a column?

£可爱£侵袭症+ 提交于 2021-01-27 21:04:18
问题 In Postgresql (version 10) , following sql select all rows order by the avg_grade . -- query - students list, order by average grade, select s.student_id, s.student_name, avg(ce.grade) as avg_grade from students as s left join course_enrollment as ce on s.student_id = ce.student_id group by s.student_id order by avg_grade desc NULLS LAST; Relevant tables students: create table students ( student_id bigserial not null primary key, student_name varchar(200) not null, created timestamp default

import data from excel to postgres in python using pyodbc

浪尽此生 提交于 2021-01-27 20:33:35
问题 I am importing data from MS-Excel to PostgreSQL in python(2.6) using pyodbc . The problem faced is: There are characters like left single quotation mark(ANSI hex code : 0x91) , etc in the excel source. Now, when it is import into PostgreSQL using pyodbc, it terminates and gives the error DatabaseError: invalid byte sequence for encoding "UTF8": 0x91 . What I tried: I used decode('unicode_escape') for the time being. But, this cannot be done as this simply removes/escapes the concerned

Recognizing invalid dates in postgresql

那年仲夏 提交于 2021-01-27 20:33:09
问题 I am trying to parse dirty input into postgres tables. I have a problem with a 'date' field occasionally containing non-dates such as '00000000' or '20100100'. pg refuses to accept these, and rightly so. Is there a way to have postgres recognize invalid dates (or only valid dates, if that works better), so I can substitute a sensible default? (I've considered building a table listing the dates I'm willing to accept, and use that in a sub-select, but that seems awfully inelegant.) Cheers,

Multiple primary keys for table “app_employee” are not allowed.

故事扮演 提交于 2021-01-27 19:59:55
问题 Django 1.11 with PostgreSQL. I go to migrate my site and models.py throws the error that I can't have more than one primary key. I can't see where I do (or I'm not understanding how). class Employee(models.Model): Aegis_ID = models.UUIDField(primary_key=True, null=False, default=uuid.uuid4, editable=False, serialize=True) Employee_Number = models.ForeignKey('self', on_delete=models.CASCADE, related_name='Company_Employee_Number', null=True, blank=True, max_length=6, help_text="Employee ID")

Postgresql: master-slave replication of 1 table

爱⌒轻易说出口 提交于 2021-01-27 19:14:22
问题 Help me to chouse a simple (lightweight) solution to the master-slave replication of one table between two Postgresql databases. The table contains a large object. 回答1: Here you'll find a very good overview of the replication tools for PostgreSQL. Please, take a look and hopefully you'll be able to pick one. Otherwise, if you need something really lightweight, you can do it yourself. You'll need a trigger and a couple of functions, a dblink module if you need almost immediate changes

POSTGRES - prevent serial incrementation with ON CONFLICT DO NOTHING [duplicate]

偶尔善良 提交于 2021-01-27 19:07:47
问题 This question already has answers here : serial in postgres is being increased even though I added on conflict do nothing (4 answers) Closed 5 months ago . duplicate of this question Say I have the following table things. I want unique names in the table so there are no duplicates. The process that inserts a thing shouldn't need to check if a thing with this name is already there. CREATE TABLE things( id SMALLSERIAL PRIMARY KEY, name varchar UNIQUE ); When I insert values like this it works.