RSS

Posts in 2020

  • PostgreSQL mini cookbook: Dealing with the system tables

    February 29, 2020 in PostgreSQL

    PostgreSQL mini cookbook: Dealing with the system tables, those tricks back to 2001 and still works today. 😉

    Seeing what tables exist You want to check if a table already exists. Solution There is a view called pg_tables or the system table pg_class - you can query either. There is less information in pg_tables but it is easier to understand. List all the …

    Read more

  • PostgreSQL mini cookbook: Constraining your data

    February 28, 2020 in PostgreSQL

    PostgreSQL mini cookbook: Constraining your data, those tricks back to 2001 and still works today. 😉

    Making sure a related record exists Records in one table should all be connected to records in another table - you shouldn't have diary entries for a company that isn't listed in the companies table. Solution Define a FOREIGN KEY on the diary table …

    Read more

  • PostgreSQL mini cookbook: Controlling Access to your data

    February 27, 2020 in PostgreSQL

    PostgreSQL mini cookbook: Controlling Access to your data, those tricks back to 2001 and still works today. 😉

    Restricting users to certain records or certain fields with views The accounts team don't need to see unfilled orders and the sales team don't need to see bank details. You want to enforce this. Solution Create one view for each group of users, and …

    Read more

  • PostgreSQL mini cookbook: Automating processes

    February 26, 2020 in PostgreSQL

    PostgreSQL mini cookbook: Automating processes, those tricks back to 2001 and still works today. 😉

    Enabling plpgsql You want to write functions and build triggers in Postgres’ scripting language plpgsql. PL/pgSQL is a loadable procedural language for the PostgreSQL database system. The design goals of PL/pgSQL were to create a loadable …

    Read more

  • PostgreSQL mini cookbook: advanced query tricks

    February 26, 2020 in PostgreSQL

    PostgreSQL mini cookbook: advanced query tricks, those tricks back to 2001 and still works today. 😉

    Having a dynamic field value without a lookup table You want a “calculated” column returned from a select but joining to a lookup table is either not practical or not desirable. Solution Use the CASE...WHEN...ELSE...END structure or write …

    Read more

  • PostgreSQL mini cookbook: aggregate query tricks

    February 25, 2020 in PostgreSQL

    PostgreSQL mini cookbook: PostgreSQL aggregate query tricks, those tricks back to 2001 and still works today. 😉

    Sum a field You want to calculate the total number of payments per customer, staff. Solution Use SUM() and GROUP BY: SELECT a, b, sum(c) FROM t GROUP BY a ,b; Example: # SELECT * FROM payment; payment_id | customer_id | staff_id | rental_id | amount …

    Read more

  • PostgreSQL mini cookbook: basic query tricks

    February 24, 2020 in PostgreSQL

    PostgreSQL mini cookbook: PostgreSQL basic query tricks, those tricks back to 2001 and still works today. 😉

    Case insensitive searches You want to check equality or do a regular expression search but ignoring case Solution Either use lower() on the field and the value being compared or use the ILIKE or ˜˜* regular expression operators. Discussion If you …

    Read more

  • Python unicode string lowercase and caseless match

    February 23, 2020 in Python

    str.lower() and str.casefold()

    str.lower() and str.casefold() Starting with Python 3.0, strings are stored as Unicode. Python defined to two functions str.lower() and str.casefold() can be used to convert string to lowercase: str.lower() Return a copy of the string with all the …

    Read more

  • psql 101

    February 20, 2020 in PostgreSQL

    psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries interactively, issue them to PostgreSQL, and see the query results. Alternatively, input can be from a file.

    psql shell command List exist databases $ psql --list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------------------+-------+----------+-------------+-------------+------------------- dvdrental | dbch | UTF8 …

    Read more

  • Azure function Did not find any initialized language workers

    February 17, 2020 in Azure

    Azure function app random response 500 Internal error to client. log indicate Azure function: Did not find any initialized language workers.

    Symptom We deployed a very simple Azure function with nodejs runtime, on an Linux container. However after deployment, the service is not stable stable, it goes up and down, server random response 500 Internal error to client. From Azure's log, it …

    Read more