Install Postgres.app on a Mac

The easiest way to install PostgreSQL on a Mac is by installing Postgres.app. Postgres.app is a full-featured PostgreSQL installation packaged as a standard Mac application.

You simply download it and drag it to your Applications folder, just like with any other application.

Read more

How to Format Numbers in PostgreSQL

When working with Postgres, you can use the to_char() function to output numbers in a given format.

The way it works is that you provide two arguments. The first argument is the number to be formatted. The second argument determines how it is formatted.

The return value is text.

Depending on your requirements, you might prefer to convert the number to the appropriate data type, for example using the cast() function.

Examples of both methods are below.

Read more

Set a Default Value for a Column in SQLite: DEFAULT Constraint

When creating a table in SQLite, you have the option of adding constraints to each column.

One such constraint is the DEFAULT constraint.

The DEFAULT constraint allows you to specify a value to be used in the event no value is supplied for that column when a new row is inserted.

If you don’t use a DEFAULT clause, then the default value for a column is NULL.

Read more

Add a Generated Column to a Table in SQLite

You can add a generated column to an existing table in SQLite by using the ALTER TABLE statement.

SQLite’s implementation of the ALTER TABLE statement is very limited, but it does allow you to add a column – including generated columns.

Generated columns (also known as “computed columns”) are columns that get their value from an expression that computes values from other columns.

Read more

How to Create a Computed Column in SQLite

Generated column support was added to SQLite in version 3.31.0, which was released on 22 January 2020.

Generated columns and computed columns are the same thing. They are columns whose values are a function of other columns in the same row.

In SQLite, generated columns are created using the GENERATED ALWAYS column-constraint when creating or altering the table.

There are two types of generated column; STORED and VIRTUAL. Only VIRTUAL columns can be added when altering a table. Both types can be added when creating a table.

Read more

Add a Column to an Existing Table in SQLite

In SQLite, you can use the ALTER TABLE statement to add a column to an existing table.

This is actually one of the few things you can do with the ALTER TABLE statement in SQLite. The only things you can do with the ALTER TABLE statement in SQLite is rename a table, rename a column, and add a new column to an existing table.

Read more

Convert NULL Values to the Column’s Default Value when Inserting Data in SQLite

One of SQLite‘s non-standard extensions to SQL is the ON CONFLICT clause.

This clause allows you to determine what should happen when certain conflicts occur due to a constraint violation.

One of the things you can use this clause for is to replace NULL values with a column’s default value when inserting or updating data in a table.

By default, if you try to explicitly insert NULL into a column with a NOT NULL constraint, it will fail.

And if you try to explicitly insert NULL into a column without a NOT NULL constraint, then NULL will be assigned to that column, even if there’s a DEFAULT clause.

However, you can use the ON CONFLICT clause to set it to the default value instead of NULL.

Read more