@@ -2,53 +2,56 @@
22
33[](https://github.com/fabianlindfors/reshape/actions/workflows/test.yaml) [](https://github.com/fabianlindfors/reshape/releases)
44
5+> Also check out [ReshapeDB](https://reshapedb.com), a new database built from the ground up to make zero-downtime schema and data migrations as simple and safe as possible. If you'd like to chat about it, please [reach out](contact@reshapedb.com)!
6+
57Reshape is an easy-to-use, zero-downtime schema migration tool for Postgres. It automatically handles complex migrations that would normally require downtime or manual multi-step changes. During a migration, Reshape ensures both the old and new schema are available at the same time, allowing you to gradually roll out your application. It will also perform all changes without excessive locking, avoiding downtime caused by blocking other queries. For a more thorough introduction to Reshape, check out the [introductory blog post](https://fabianlindfors.se/blog/schema-migrations-in-postgres-using-reshape/).
68
79Designed for Postgres 12 and later.
810
9−*Note: Reshape is **experimental** and should not be used in production. It can (and probably will) break your application.*
10−
1111- [How it works](#how-it-works)
1212- [Getting started](#getting-started)
13− - [Installation](#installation)
14− - [Creating your first migration](#creating-your-first-migration)
15− - [Preparing your application](#preparing-your-application)
16− - [Running your migration](#running-your-migration)
17− - [Using during development](#using-during-development)
13+ - [Installation](#installation)
14+ - [Creating your first migration](#creating-your-first-migration)
15+ - [Preparing your application](#preparing-your-application)
16+ - [Running your migration](#running-your-migration)
17+ - [Using during development](#using-during-development)
1818- [Writing migrations](#writing-migrations)
19− - [Basics](#basics)
20− - [Tables](#tables)
21− - [Create table](#create-table)
22− - [Rename table](#rename-table)
23− - [Remove table](#remove-table)
24− - [Columns](#columns)
25− - [Add column](#add-column)
26− - [Alter column](#alter-column)
27− - [Remove column](#remove-column)
28− - [Indices](#indices)
29− - [Add index](#add-index)
30− - [Remove index](#remove-index)
31− - [Enums](#enums)
32− - [Create enum](#create-enum)
33− - [Remove enum](#remove-enum)
19+ - [Basics](#basics)
20+ - [Tables](#tables)
21+ - [Create table](#create-table)
22+ - [Rename table](#rename-table)
23+ - [Remove table](#remove-table)
24+ - [Add foreign key](#add-foreign-key)
25+ - [Remove foreign key](#remove-foreign-key)
26+ - [Columns](#columns)
27+ - [Add column](#add-column)
28+ - [Alter column](#alter-column)
29+ - [Remove column](#remove-column)
30+ - [Indices](#indices)
31+ - [Add index](#add-index)
32+ - [Remove index](#remove-index)
33+ - [Enums](#enums)
34+ - [Create enum](#create-enum)
35+ - [Remove enum](#remove-enum)
36+ - [Custom](#custom)
37+ - [Complex changes across tables](#complex-changes-across-tables)
3438- [Commands and options](#commands-and-options)
35− - [`reshape migrate`](#reshape-migrate)
36− - [`reshape complete`](#reshape-complete)
37− - [`reshape abort`](#reshape-abort)
38− - [`reshape generate-schema-query`](#reshape-generate-schema-query)
39− - [Connection options](#connection-options)
39+ - [`reshape migration start`](#reshape-migration-start)
40+ - [`reshape migration complete`](#reshape-migration-complete)
41+ - [`reshape migration abort`](#reshape-migration-abort)
42+ - [`reshape schema-query`](#reshape-schema-query)
43+ - [Connection options](#connection-options)
4044- [License](#license)
4145
42−
4346## How it works
4447
4548Reshape works by creating views that encapsulate the underlying tables, which your application will interact with. During a migration, Reshape will automatically create a new set of views and set up triggers to translate inserts and updates between the old and new schema. This means that every deployment is a three-phase process:
4649
47−1. **Start migration** (`reshape migrate`): Sets up views and triggers to ensure both the new and old schema are usable at the same time.
50+1. **Start migration** (`reshape migration start`): Sets up views and triggers to ensure both the new and old schema are usable at the same time.
48512. **Roll out application**: Your application can be gradually rolled out without downtime. The existing deployment will continue using the old schema whilst the new deployment uses the new schema.
49−3. **Complete migration** (`reshape complete`): Removes the old schema and any intermediate data and triggers.
52+3. **Complete migration** (`reshape migration complete`): Removes the old schema and any intermediate data and triggers.
5053
51−If the application deployment fails, you should run `reshape abort` which will roll back any changes made by `reshape migrate` without losing data.
54+If the application deployment fails, you should run `reshape migration abort` which will roll back any changes made by `reshape migration start` without losing data.
5255
5356## Getting started
5457
@@ -60,7 +63,7 @@ Binaries are available for macOS and Linux under [Releases](https://github.com/f
6063
6164#### Cargo
6265
63−Reshape can be installed using [Cargo](https://doc.rust-lang.org/cargo/):
66+Reshape can be installed using [Cargo](https://doc.rust-lang.org/cargo/) (requires Rust 1.58 or later):
6467
6568```shell
6669cargo install reshape
@@ -68,10 +71,10 @@ cargo install reshape
6871
6972#### Docker
7073
71−Reshape is available as a Docker image on [Docker Hub](https://hub.docker.com/repository/docker/fabianlindfors/reshape).
74+Reshape is available as a Docker image on [Docker Hub](https://hub.docker.com/repository/docker/fabianlindfors/reshape).
7275
7376```shell
74−docker run -v $(pwd):/usr/share/app fabianlindfors/reshape reshape migrate
77+docker run -v $(pwd):/usr/share/app fabianlindfors/reshape reshape migration start
7578```
7679
7780### Creating your first migration
@@ -100,9 +103,14 @@ This is the equivalent of running `CREATE TABLE users (id INTEGER GENERATED ALWA
100103
101104### Preparing your application
102105
103−Reshape relies on your application using a specific schema. When establishing the connection to Postgres in your application, you need to run a query to select the most recent schema. This query can be generated using: `reshape generate-schema-query`.
106+Reshape relies on your application using a specific schema. When establishing the connection to Postgres in your application, you need to run a query to select the most recent schema. The simplest way to do this is to use one of the helper libraries:
104107
105−To pass it along to your application, you can for example use an environment variable in your run script: `RESHAPE_SCHEMA_QUERY=$(reshape generate-schema-query)`. Then in your application:
108+- [Rust](https://github.com/fabianlindfors/reshape-helper)
109+- [Ruby (and Rails)](https://github.com/fabianlindfors/reshape-ruby)
110+- [Python (and Django)](https://github.com/fabianlindfors/reshape-python)
111+- [Go](https://github.com/leourbina/reshape-helper)
112+
113+If your application is not using one of the languages with an available helper library, you can instead generate the query with the command: `reshape schema-query`. To pass it along to your application, you can for example use an environment variable in your run script: `RESHAPE_SCHEMA_QUERY=$(reshape schema-query)`. Then in your application:
106114
107115```python
108116# Example for Python
@@ -115,16 +123,16 @@ db.execute(reshape_schema_query)
115123To create your new `users` table, run:
116124
117125```bash
118−reshape migrate
126+reshape migration start --complete
119127```
120128
121−As this is the first migration, Reshape will automatically complete it. For subsequent migrations, you will need to first run `reshape migrate`, roll out your application and then complete the migration using `reshape complete`.
129+We use the `--complete` flag to automatically complete the migration. During a production deployment, you should first run `reshape migration start` followed by `reshape migration complete` once your application has been fully rolled out.
122130
123131If nothing else is specified, Reshape will try to connect to a Postgres database running on `localhost` using `postgres` as both username and password. See [Connection options](#connection-options) for details on how to change the connection settings.
124132
125133### Using during development
126134
127−When adding new migrations during development, we recommend running `reshape migrate` but skipping `reshape complete`. This way, the new migrations can be iterated on by updating the migration file and running `reshape abort` followed by `reshape migrate`.
135+When adding new migrations during development, we recommend running `reshape migration start` but skipping `reshape migration complete`. This way, the new migrations can be iterated on by updating the migration file and running `reshape migration abort` followed by `reshape migration start`.
128136
129137## Writing migrations
130138
@@ -159,9 +167,9 @@ Every action has a `type`. The supported types are detailed below.
159167
160168#### Create table
161169
162−The `create_table` action will create a new table with the specified columns, indices and constraints.
170+The `create_table` action will create a new table with the specified columns, indices and constraints. You can optionally provide an `up` option to backfill values from an existing table.
163171
164−*Example: create a `customers` table with a few columns and a primary key*
172+_Example: create a `customers` table with a few columns and a primary key_
165173
166174```toml
167175[[actions]]
@@ -179,13 +187,13 @@ primary_key = ["id"]
179187 type = "TEXT"
180188
181189 # Columns default to nullable
182− nullable = false
190+ nullable = false
183191
184192 # default can be any valid SQL value, in this case a string literal
185− default = "'PLACEHOLDER'"
193+ default = "'PLACEHOLDER'"
186194```
187195
188−*Example: create `users` and `items` tables with a foreign key between them*
196+_Example: create `users` and `items` tables with a foreign key between them_
189197
190198```toml
191199[[actions]]
@@ -218,11 +226,34 @@ primary_key = ["id"]
218226 referenced_columns = ["id"]
219227```
220228
229+_Example: create `profiles` table based on existing `users` table_
230+
231+```toml
232+[[actions]]
233+type = "create_table"
234+name = "profiles"
235+primary_key = ["user_id"]
236+
237+ [[actions.columns]]
238+ name = "user_id"
239+ type = "INTEGER"
240+
241+ [[actions.columns]]
242+ name = "user_email"
243+ type = "TEXT"
244+
245+ # Backfill from `users` table and copy `users.email` to `user_email` column
246+ # This will perform an upsert based on the primary key to avoid duplicate rows
247+ [actions.up]
248+ table = "users"
249+ values = { user_id = "id", user_email = "email" }
250+```
251+
221252#### Rename table
222253
223254The `rename_table` action will change the name of an existing table.
224255
225−*Example: change name of `users` table to `customers`*
256+_Example: change name of `users` table to `customers`_
226257
227258```toml
228259[[actions]]
@@ -235,7 +266,7 @@ new_name = "customers"
235266
236267The `remove_table` action will remove an existing table.
237268
238−*Example: remove `users` table*
269+_Example: remove `users` table_
239270
240271```toml
241272[[actions]]
@@ -243,13 +274,43 @@ type = "remove_table"
243274table = "users"
244275```
245276
277+#### Add foreign key
278+
279+The `add_foreign_key` action will add a foreign key between two existing tables. The migration will fail if the existing column values aren't valid references.
280+
281+_Example: create foreign key from `items` to `users` table_
282+
283+```toml
284+[[actions]]
285+type = "add_foreign_key"
286+table = "items"
287+
288+ [actions.foreign_key]
289+ columns = ["user_id"]
290+ referenced_table = "users"
291+ referenced_columns = ["id"]
292+```
293+
294+#### Remove foreign key
295+
296+The `remove_foreign_key` action will remove an existing foreign key. The foreign key will only be removed once the migration is completed, which means that your new application must continue to adhere to the foreign key constraint.
297+
298+_Example: remove foreign key `items_user_id_fkey` from `users` table_
299+
300+```toml
301+[[actions]]
302+type = "remove_foreign_key"
303+table = "items"
304+foreign_key = "items_user_id_fkey"
305+```
306+
246307### Columns
247308
248309#### Add column
249310
250−The `add_column` action will add a new column to an existing table. You can optionally provide an `up` setting. This should be an SQL expression which will be run for all existing rows to backfill the new column.
311+The `add_column` action will add a new column to an existing table. You can optionally provide an `up` setting. This should be an SQL expression which will be run for all existing rows to backfill the new column. `up` may also reference another table to perform cross-table migrations (see ["Complex changes across tables"](#complex-changes-across-tables)).
251312
252−*Example: add a new column `reference` to table `products`*
313+_Example: add a new column `reference` to table `products`_
253314
254315```toml
255316[[actions]]
@@ -263,7 +324,7 @@ table = "products"
263324 default = "10"
264325```
265326
266−*Example: replace an existing `name` column with two new columns, `first_name` and `last_name`*
327+_Example: replace an existing `name` column with two new columns, `first_name` and `last_name`_
267328
268329```toml
269330[[actions]]
@@ -299,14 +360,48 @@ column = "name"
299360down = "first_name || ' ' || last_name"
300361```
301362
363+_Example: extract nested value from unstructured JSON `data` column to new `name` column_
364+
365+```toml
366+[[actions]]
367+type = "add_column"
368+table = "users"
369+
370+# #>> '{}' converts the JSON string value to TEXT
371+up = "data['path']['to']['value'] #>> '{}'"
372+
373+ [actions.column]
374+ name = "name"
375+ type = "TEXT"
376+```
377+
378+_Example: duplicate `email` column from `users` to `profiles` table_
379+
380+```toml
381+# `profiles` has `user_id` column which maps to `users.id`
382+[[actions]]
383+type = "add_column"
384+table = "profiles"
385+
386+ [actions.column]
387+ name = "email"
388+ type = "TEXT"
389+ nullable = false
390+
391+ # When `users` is updated in the old schema, we write the email value to `profiles`
392+ [actions.up]
393+ table = "users"
394+ value = "email"
395+ where = "user_id = id"
396+```
302397
303398#### Alter column
304399
305400The `alter_column` action enables many different changes to an existing column, for example renaming, changing type and changing existing values.
306401
307−When performing more complex changes than a rename, `up` and `down` must be provided. These should be SQL expressions which determine how to transform between the new and old version of the column. Inside those expressions, you can reference the current column value by the column name.
402+When performing more complex changes than a rename, `up` and `down` should be provided. These should be SQL expressions which determine how to transform between the new and old version of the column. Inside those expressions, you can reference the current column value by the column name.
308403
309−*Example: rename `last_name` column on `users` table to `family_name`*
404+_Example: rename `last_name` column on `users` table to `family_name`_
310405
311406```toml
312407[[actions]]
@@ -318,7 +413,7 @@ column = "last_name"
318413 name = "family_name"
319414```
320415
321−*Example: change the type of `reference` column from `INTEGER` to `TEXT`*
416+_Example: change the type of `reference` column from `INTEGER` to `TEXT`_
322417
323418```toml
324419[[actions]]
@@ -333,7 +428,7 @@ down = "CAST(reference AS INTEGER)" # Converts from text value to integer
333428 type = "TEXT" # Previous type was 'INTEGER'
334429```
335430
336−*Example: increment all values of an `index` column by one*
431+_Example: increment all values of an `index` column by one_
337432
338433```toml
339434[[actions]]
@@ -348,7 +443,22 @@ down = "index - 1" # Decrement to revert for old schema
348443 name = "index"
349444```
350445
351−*Example: change default value of `created_at` column to current time*
446+_Example: make `name` column not nullable_
447+
448+```toml
449+[[actions]]
450+type = "alter_column"
451+table = "users"
452+column = "name"
453+
454+# Use "N/A" for any rows that currently have a NULL name
455+up = "COALESCE(name, 'N/A')"
456+
457+ [actions.changes]
458+ nullable = false
459+```
460+
461+_Example: change default value of `created_at` column to current time_
352462
353463```toml
354464[[actions]]
@@ -362,11 +472,11 @@ column = "created_at"
362472
363473#### Remove column
364474
365−The `remove_column` action will remove an existing column from a table. You can optionally provide a `down` setting. This should be an SQL expression which will be used to determine values for the old schema when inserting or updating rows using the new schema. The `down` setting must be provided when the removed column is `NOT NULL` or doesn't have a default value.
475+The `remove_column` action will remove an existing column from a table. You can optionally provide a `down` setting. This should be an SQL expression which will be used to determine values for the old schema when inserting or updating rows using the new schema. `down` may also reference another table to perform cross-table migrations (see ["Complex changes across tables"](#complex-changes-across-tables)) . The `down` setting must be provided when the removed column is `NOT NULL` or doesn't have a default value.
366476
367477Any indices that cover the column will be removed.
368478
369−*Example: remove column `name` from table `users`*
479+_Example: remove column `name` from table `users`_
370480
371481```toml
372482[[actions]]
@@ -378,18 +488,34 @@ column = "name"
378488down = "'N/A'"
379489```
380490
491+_Example: remove `email` column from `users` table and use column from `profiles` table instead_
492+
493+```toml
494+[[actions]]
495+type = "remove_column"
496+table = "users"
497+column = "email"
498+
499+ # Our application will use the `profiles.email` column instead
500+ # For backwards compatibility, we will write back to the removed `email` column whenever `profiles` is changed
501+ [actions.down]
502+ table = "profiles"
503+ value = "profiles.email"
504+ where = "users.id = profiles.user_id"
505+```
506+
381507### Indices
382508
383509#### Add index
384510
385511The `add_index` action will add a new index to an existing table.
386512
387−*Example: create a `users` table with a unique index on the `name` column*
513+_Example: create a `users` table with a unique index on the `name` column_
388514
389515```toml
390516[[actions]]
391517type = "create_table"
392−table = "users"
518+name = "users"
393519primary_key = "id"
394520
395521 [[actions.columns]]
@@ -413,7 +539,7 @@ table = "users"
413539 unique = true
414540```
415541
416−*Example: add GIN index to `data` column on `products` table*
542+_Example: add GIN index to `data` column on `products` table_
417543
418544```toml
419545[[actions]]
@@ -432,7 +558,7 @@ table = "products"
432558
433559The `remove_index` action will remove an existing index. The index won't actually be removed until the migration is completed.
434560
435−*Example: remove the `name_idx` index*
561+_Example: remove the `name_idx` index_
436562
437563```toml
438564[[actions]]
@@ -446,7 +572,7 @@ index = "name_idx"
446572
447573The `create_enum` action will create a new [enum type](https://www.postgresql.org/docs/current/datatype-enum.html) with the specified values.
448574
449−*Example: add a new `mood` enum type with three possible values*
575+_Example: add a new `mood` enum type with three possible values_
450576
451577```toml
452578[[actions]]
@@ -459,7 +585,7 @@ values = ["happy", "ok", "sad"]
459585
460586The `remove_enum` action will remove an existing [enum type](https://www.postgresql.org/docs/current/datatype-enum.html). Make sure all usages of the enum has been removed before running the migration. The enum will only be removed once the migration is completed.
461587
462−*Example: remove the `mood` enum type*
588+_Example: remove the `mood` enum type_
463589
464590```toml
465591[[actions]]
@@ -467,61 +593,178 @@ type = "remove_enum"
467593enum = "mood"
468594```
469595
596+### Custom
597+
598+The `custom` action lets you create a migration which runs custom SQL. It should be used with great care as it provides no guarantees of zero-downtime and will simply run whatever SQL is provided. Use other actions whenever possible as they are explicitly designed for zero downtime.
599+
600+There are three optional settings available which all accept SQL queries. All queries need to be idempotent, for example by using `IF NOT EXISTS` wherever available.
601+
602+- `start`: run when a migration is started using `reshape migration start`
603+- `complete`: run when a migration is completed using `reshape migration complete`
604+- `abort`: run when a migration is aborted using `reshape migration abort`
605+
606+_Example: enable PostGIS and pg_stat_statements extensions_
607+
608+```toml
609+[[actions]]
610+type = "custom"
611+
612+start = """
613+ CREATE EXTENSION IF NOT EXISTS postgis;
614+ CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
615+"""
616+
617+abort = """
618+ DROP EXTENSION IF EXISTS postgis;
619+ DROP EXTENSION IF EXISTS pg_stat_statements;
620+"""
621+```
622+
623+### Complex changes across tables
624+
625+The `up` and `down` options available when creating tables, adding columns and removing columns can also perform more complex changes that span tables.
626+
627+_Example: move `email` column from `users` to `profiles` table_
628+
629+```toml
630+[[actions]]
631+type = "add_column"
632+table = "profiles"
633+
634+ [actions.column]
635+ name = "email"
636+ type = "TEXT"
637+ nullable = false
638+
639+ # When `users` is updated in the old schema, we write the email value to `profiles`
640+ [actions.up]
641+ table = "users"
642+ value = "users.email"
643+ where = "profiles.user_id = users.id"
644+
645+[[actions]]
646+type = "remove_column"
647+table = "users"
648+column = "email"
649+
650+ # When `profiles` is changed in the new schema, we write the email address back to the removed column
651+ [actions.down]
652+ table = "profiles"
653+ value = "profiles.email"
654+ where = "users.id = profiles.user_id"
655+```
656+
657+_Example: turn a 1:N relationship between `users` and `accounts` into N:M and change the format of the associated `role`_
658+
659+```toml
660+# Add `user_account_connections` as a junction table
661+[[actions]]
662+type = "create_table"
663+name = "user_account_connections"
664+primary_key = ["account_id", "user_id"]
665+
666+ [[actions.columns]]
667+ name = "account_id"
668+ type = "INTEGER"
669+
670+ [[actions.columns]]
671+ name = "user_id"
672+ type = "INTEGER"
673+
674+ # `role` is currently stored directly on the `users` table but is part of the relationship
675+ [[actions.columns]]
676+ name = "role"
677+ type = "TEXT"
678+ nullable = false
679+
680+ # Backfill the new table from `users` and uppercase the `role`
681+ [actions.up]
682+ table = "users"
683+ values = { user_id = "id", account_id = "account_id", role = "UPPER(account_role)" }
684+ where = "user_account_connections.user_id = users.id"
685+
686+[[actions]]
687+type = "remove_column"
688+table = "users"
689+column = "account_id"
690+
691+ # When `user_account_connections` is updated, we write the `account_id` back to `users`
692+ [actions.down]
693+ table = "user_account_connections"
694+ value = "user_account_connections.account_id"
695+ where = "users.id = user_account_connections.user_id"
696+
697+[[actions]]
698+type = "remove_column"
699+table = "users"
700+column = "account_role"
701+
702+ # When `user_account_connections` is updated, we write the lowercase role back to `users`
703+ [actions.down]
704+ table = "user_account_connections"
705+ value = "LOWER(user_account_connections.role)"
706+ where = "users.id = user_account_connections.user_id"
707+```
708+
470709## Commands and options
471710
472−### `reshape migrate`
711+### `reshape migration start`
473712
474−Starts a new migration, applying all migrations under `migrations/` that haven't yet been applied. After the command has completed, both the old and new schema will be usable at the same time. When you have rolled out the new version of your application which uses the new schema, you should run `reshape complete`.
713+Starts a new migration, applying all migrations under `migrations/` that haven't yet been applied. After the command has completed, both the old and new schema will be usable at the same time. When you have rolled out the new version of your application which uses the new schema, you should run `reshape migration complete`.
475714
476715#### Options
477716
478−*See also [Connection options](#connection-options)*
717+_See also [Connection options](#connection-options)_
479718
480−| Option | Default | Description |
481−| ------ | ------- | ----------- |
482−| `--complete`, `-c` | `false` | Automatically complete migration after applying it. |
483−| `--dirs` | `migrations/` | Directories to search for migration files. Multiple directories can be specified using `--dirs dir1 dir2 dir3`. |
719+| Option | Default | Description |
720+| ------------------ | ------------- | --------------------------------------------------------------------------------------------------------------- |
721+| `--complete`, `-c` | `false` | Automatically complete migration after applying it. |
722+| `--dirs` | `migrations/` | Directories to search for migration files. Multiple directories can be specified using `--dirs dir1 dir2 dir3`. |
484723
485−### `reshape complete`
724+### `reshape migration complete`
486725
487−Completes migrations previously started with `reshape complete`.
726+Completes migrations previously started with `reshape migration complete`.
488727
489728#### Options
490729
491730See [Connection options](#connection-options)
492731
493−### `reshape abort`
732+### `reshape migration abort`
494733
495−Aborts any migrations which haven't yet been completed.
734+Aborts any migrations which haven't yet been completed.
496735
497736#### Options
498737
499738See [Connection options](#connection-options)
500739
501−### `reshape generate-schema-query`
740+### `reshape schema-query`
502741
503742Generates the SQL query you need to run in your application before using the database. This command does not require a database connection. Instead it will generate the query based on the latest migration in the `migrations/` directory (or the directories specified by `--dirs`).
504743
744+If your application is written in Rust, we recommend using the [Rust helper library](https://github.com/fabianlindfors/reshape-helper/) instead.
745+
505746The query should look something like `SET search_path TO migration_1_initial_migration`.
506747
507748#### Options
508749
509−| Option | Default | Description |
510−| ------ | ------- | ----------- |
750+| Option | Default | Description |
751+| -------- | ------------- | --------------------------------------------------------------------------------------------------------------- |
511752| `--dirs` | `migrations/` | Directories to search for migration files. Multiple directories can be specified using `--dirs dir1 dir2 dir3`. |
512753
513754### Connection options
514755
515−The options below can be used with all commands that communicate with Postgres. Use either a connection URL or specify each connection option individually.
756+The options below can be used with all commands that communicate with Postgres. Use either a [connection URL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) or specify each connection option individually.
757+
758+All options can also be set using environment variables instead of flags. If a `.env` file exists, then variables will be automatically loaded from there.
516759
517−| Option | Default | Description |
518−| ------ | ------- | ----------- |
519−| `--url` | | URI to connect to your Postgres database.<br>Can also be provided with the environment variable `DATABASE_URL`. |
520−| `--host` | `localhost` | Hostname to use when connecting to Postgres |
521−| `--port` | `5432` | Port which Postgres is listening on |
522−| `--database` | `postgres` | Database name |
523−| `--username` | `postgres` | Postgres username |
524−| `--password` | `postgres` | Postgres password |
760+| Option | Default | Environment variable | Description |
761+| ------------ | ----------- | -------------------- | ------------------------------------------- |
762+| `--url` | | `DB_URL` | URL to your Postgres database |
763+| `--host` | `localhost` | `DB_HOST` | Hostname to use when connecting to Postgres |
764+| `--port` | `5432` | `DB_PORT` | Port which Postgres is listening on |
765+| `--database` | `postgres` | `DB_NAME` | Database name |
766+| `--username` | `postgres` | `DB_USERNAME` | Postgres username |
767+| `--password` | `postgres` | `DB_PASSWORD` | Postgres password |
525768
526769## License
527770