postgresql

How to distribute values when prior rank is zero

眉间皱痕 提交于 2021-01-29 05:45:30
问题 Not certain how to title this question. Describing is a bit of a challenge, too. I have shaped my postgres data as follows, where a location and geo is further broken out into micro geo's based on dist_grp column. Dist_grp values range from 1 to 6 and have fixed allocation values (1=0.55, 2=0.18, 3=0.15, 4=0.09, 5=0.025, 6=0.005). So, the sum of the allocation for each geo and location combination is 1. I don't have any problems as long as there are no gaps in data. I have found that not all

PostgreSQL: Copy/import a user defined function from one database to another database

旧街凉风 提交于 2021-01-29 05:41:37
问题 I have a similar question like this, but for PostgreSQL. I am using PostgreSQL 9.5.12 (x64) on my Windows 10 (x64) machine. I have a function (written in plpgsql ) my_func() in database my_db . Is it possible to import/copy this function to another database in postgres? 回答1: In psql the \ef command will open the function in an editor you can then save it to a file... unfortunately the UI of the windows version of psql was pretty cruddy last time I checked. a pg_dump of the database with -

Unable to insert nested record in postgres

∥☆過路亽.° 提交于 2021-01-29 05:40:16
问题 i had managed to create tables in postgres but encountered issues when trying to insert values. comands = ( CREATE TYPE student AS ( name TEXT, id INTEGER ) CREATE TABLE studentclass( date DATE NOT NULL, time TIMESTAMPTZ NOT NULL, PRIMARY KEY (date, time), class student ) ) And in psycog2 command = ( INSERT INTO studentclass (date, time, student) VALUES (%s,%s, ROW(%s,%s)::student) ) student_rec = ("John", 1) record_to_insert = ("2020-05-21", "2020-05-21 08:10:00", student_rec) cursor.execute

Getting incorrect answer to formula when adding column to table in SQL

社会主义新天地 提交于 2021-01-29 05:36:48
问题 My table looks like this: error | num | date -------+-------+------------ 274 | 38431 | 2016-07-01 389 | 54811 | 2016-07-02 401 | 54465 | 2016-07-03 I want to calculate on which dates the 'error' was more than 1% of the 'num'. Currently I'm trying to do this with this expression: select date, error, num, (100 * (error / num)) as percentage from DD limit 3; The result I'm getting is incorrect: date | error | num | percentage ------------+-------+-------+------------ 2016-07-01 | 274 | 38431 |

Invalid input syntax for type timestamp: “NOW() - INTERVAL '12 hours'”

人盡茶涼 提交于 2021-01-29 05:31:38
问题 I'm trying to make a query based on a field ``time` to get the last week data: Measure::where('time', '>', "NOW() - INTERVAL '12 hours'")->get(); but I can't make it work. I get this message: SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: "NOW() - INTERVAL '12 hours'" (SQL: select * from "measures" where "time" > NOW() - INTERVAL '12 hours') Thing is I get this condition from official docs of timescaleDB, supposed to be postgres compatible: SELECT

psql authentication fails with --host=localhost

半城伤御伤魂 提交于 2021-01-29 05:30:42
问题 With the command: psql --dbname=mattermost --username=mmuser --password then if I place the password it succeeds, but if I write: psql --host=localhost --dbname=mattermost --username=mmuser --password then the same password gives authentication fails How can I resolve this? 回答1: So in this one try using psql --host=127.0.0.1 --dbname=mattermost --username=mmuser --password check the pg_hba.conf for further info. So if below line in your pg_hba.conf file host all all localhost trust then it

How to perform unit tests with h2 database where bpchar is used?

送分小仙女□ 提交于 2021-01-29 05:28:40
问题 I have a spring + hibernate application that uses postgres database. I need to write unit tests for the controllers. For tests I wanted to use h2 database but unfortunately test crashes during create-drop leaving me with information that bpchar data type is invalid. I wonder how to solve this issue so I could run tests. I can't change my columns with bpchar to varchar, it need to stay as it is. I also tried to set postgresql mode but it didn't helped. Am I right that the only solution I have

Django error: Value too long for type character varying(2)

烈酒焚心 提交于 2021-01-29 05:27:23
问题 I did a database flush to reset my database. I am getting this error when I try to do the following code. Code that throws error: try: print("Attempting to load %s" % store.get('name')) # THIS NEXT LINE THROWS ERROR store_obj = Store.objects.get(name=store.get('name')) except Store.DoesNotExist: store_obj = Store(name=store.get('name'), last_updated=last_updated, address=store.get('address'), city=store.get('city'), state=store.get('state'), zip_code=store.get('zip_code')) Error: Exception

Adding registry tables to db:pg:staff “C:/Program Files/PostgreSQL/12/bin/psql” failed to start

你。 提交于 2021-01-29 05:19:20
问题 When I try to deploy my postgres database using sqitch, I face this issue adding registry tables to db:pg:staff: C:/Program Files/PostgreSQL/12/bin/psql" failed to start: "The filename, directory name, or volume label syntax is incorrect" But my psql in this path C:\Program Files\pgsql\bin . Where I should change this path, psql command added on environment variable 回答1: this issue solved by added psql environment variable with your correct path added it in your project sqitch conf to be like

Django - get latest object in each relation

纵饮孤独 提交于 2021-01-29 05:14:48
问题 Let's assume I have a Product model in my project: class Product(models.Model): price = models.IntegerField() and I want to have some sort of statistics (let's say I want to keep track of how has the price changed over time) for it: class ProductStatistics(models.Model): created = models.DateTimeField(auto_add_now=True) statistics_value = models.IntegerField() product = models.ForeignKey(Product) @classmethod def create_for_product(cls, product_ids): statistics = [] products = Product.objects