└── docs └── README.md /docs/README.md: -------------------------------------------------------------------------------- 1 | # psql command line tutorial and cheat sheet 2 | 3 | You've installed PostgreSQL. Now what? I assume you've been given a task that 4 | uses `psql` and you want to learn the absolute minimum to 5 | get the job done. 6 | 7 | This is both a brief tutorial and a 8 | quick reference for the absolute least you need to know about `psql`. 9 | I assume you're familiar with the command line and have a rough idea about 10 | what database administration tasks, but aren't familiar with how to 11 | use `psql` to do the basics. 12 | 13 | View on [GitHub Pages](https://tomcam.github.io/postgres/) or directly on [GitHub](https://github.com/tomcam/postgres/) 14 | 15 | 16 | The [PostgreSQL documentation](https://www.postgresql.org/docs/manuals/) is incredibly 17 | well written and thorough, but frankly, I didn't know where to start reading. This 18 | is my answer to that problem. 19 | 20 | If you have any complaints or suggestions please let me know by 21 | sending your feedback to tomcampbell@gmail.com. 22 | 23 | It shows how to do the following at the `psql` prompt: 24 | 25 | * [Start and quit `psql`](#opening-a-connection-locally) 26 | * [Get help](#h-help) 27 | * [Get information about databases](#getting-information-about-databases) 28 | * [Create databases](#creating-a-database) 29 | * [CREATE TABLEs](#creating-a-table-create-table) 30 | * [INSERT, or add records to a table](#adding-a-record-insert-into) 31 | * [SELECT, to do simple queries](#select) 32 | * [Reference](#reference) pointing to the official PostgreSQL documentation 33 | 34 | If you don't have access to a live PostgreSQL installation at the moment we still have your back. 35 | You can follow through the examples and the output is shown as if you 36 | did type everything out. 37 | 38 | ## The psql command line utility 39 | 40 | Many administrative tasks can or should be done on your local machine, 41 | even though if database lives on the cloud. 42 | You can do some of them through a visual user interface, but that's not covered here. 43 | Knowing how to perform these operations on the command line means you can script them, 44 | and scripting means you can automate tests, check errors, and do data entry on the command line. 45 | 46 | This section isn't a full cheat sheet for `psql`. 47 | It covers the most common operations and shows them roughly in sequence, 48 | as you'd use them in a typical work session. 49 | 50 | | Starting and quitting the psql interactive terminal | 51 | | --- | 52 | | [Command-line prompts for psql](#using-psql) | 53 | | [Quitting psql](#quitting-pqsql) | 54 | | [Opening a connection locally](#opening-a-connection-locally) | 55 | | [Opening a connection remotely](#opening-a-connection-remotely) | 56 | | [Using the psql prompt](#looking-at-the-psql-prompt) | 57 | | **Getting information about databases** | 58 | | [\h Help](#h-help) | 59 | | [\l List databases](#l-list-databases) | 60 | | [\c Connect to a database](#c-connect-to-a-database) | 61 | | [\dt Display tables](#dt-display-tables) | 62 | | [\d and \d+ Display columns (field names) of a table](#d-and-d-display-columns-field-names-of-a-table) | 63 | | [\du Display user roles](#du-display-user-roles) | 64 | | **Creating and using tables and records** | 65 | | [Creating a database](#creating-a-database) | 66 | | [Creating a table (CREATE TABLE)](#creating-a-table-create-table) | 67 | | [Adding a record (INSERT INTO)](#adding-a-record-insert-into) | 68 | | [Inserting several records at once (INSERT INTO)](#adding-inserting-several-records-at-once) | 69 | | [Adding only specific fields from a record](#adding-only-specific-columns-fields-from-a-record) | 70 | | [Doing a simple query--get a list of records (SELECT)](#doing-a-simple-query--get-a-list-of-records-select) | 71 | | **Maintenance and operations** | 72 | | [Timing](#timing) | 73 | | [Watch](#watch) | 74 | | [Maintenance](#maintenance) | 75 | 76 | 77 | 78 | ## What you need to know 79 | 80 | Before using this section, you'll need: 81 | 82 | * The user name and password for your PostgreSQL database 83 | * The IP address of your remote instance 84 | 85 | ### Command-line prompts on the operating system 86 | 87 | The `$` starting a command line in the examples below represents your operating system prompt. 88 | Prompts are configurable so it may well not look like this. 89 | On Windows it might look like `C:\Program Files\PostgreSQL>` but Windows prompts are also configurable. 90 | 91 | ```` 92 | $ psql -U sampleuser -h localhost 93 | ```` 94 | 95 | A line starting with `#` represents a comment. Same for everything to the right of a `#`. 96 | If you accidentally type it or copy and paste it in, don't worry. Nothing will happen. 97 | 98 | ````bash 99 | This worked to connect to Postgres on DigitalOcean 100 | # -U is the username (it will appear in the \l command) 101 | # -h is the name of the machine where the server is running. 102 | # -p is the port where the database listens to connections. Default is 5432. 103 | # -d is the name of the database to connect to. I think DO generated this for me, or maybe PostgreSQL. 104 | # Password when asked is csizllepewdypieiib 105 | $ psql -U doadmin -h production-sfo-test1-do-user-4866002-0.db.ondigitalocean.com -p 25060 -d mydb 106 | 107 | # Open a database in a remote location. 108 | $ psql -U sampleuser -h production-sfo-test1-do-user-4866002-0.db.ondigitalocean.com -p 21334 109 | ```` 110 | ## Using psql 111 | 112 | You'll use `psql` (aka the [PostgreSQL interactive terminal](https://www.postgresql.org/docs/current/app-psql.html)) most of all because it's used to create databases and tables, show information about tables, and even to enter information (records) into the database. 113 | 114 | ### Quitting pqsql 115 | 116 | Before we learn anything else, here's how to quit `psql` and return to the operating system prompt. 117 | You type backslash, the letter `q`, and then you press the Enter or return key. 118 | 119 | ```` 120 | # Press enter after typing \q 121 | # Remember this is backslash, not forward slash 122 | postgres=# \q 123 | ```` 124 | 125 | This takes you back out to the operating system prompt. 126 | 127 | ### Opening a connection locally 128 | 129 | A common case during development is opening a connection to a local database (one on your own machine). 130 | Run `psql` with `-U` (for user name) followed by the name of the database, `postgres` in this example: 131 | ```` 132 | # Log into Postgres as the user named postgres 133 | $ psql -U postgres 134 | ```` 135 | 136 | ### Opening a connection remotely 137 | 138 | To connect your remote PostgreSQL instance from your local machine, use `psql` at your operating system command line. 139 | Here's a typical connection. 140 | 141 | ````bash 142 | # -U is the username (it will appear in the \l command) 143 | # -h is the name of the machine where the server is running. 144 | # -p is the port where the database listens to connections. Default is 5432. 145 | # -d is the name of the database to connect to. I think DO generated this for me, or maybe PostgreSQL. 146 | $ psql -U doadmin -h production-sfo-test1-do-user-4866002-0.db.ondigitalocean.com -p 25060 -d defaultdb 147 | ```` 148 | 149 | Here you'd enter the password. In case someone is peering over your shoulder, the characters are hidden. After you've entered your information properly you'll get this message (truncated for clarity): 150 | 151 | ### Looking at the psql prompt 152 | A few things appear, then the `psql` prompt is displayed. 153 | The name of the current database appears before the prompt. 154 | 155 | ```` 156 | psql (11.1, server 11.0) 157 | Type "help" for help. 158 | 159 | postgres=# 160 | ```` 161 | 162 | At this point you're expected to type commands and parameters into the command line. 163 | 164 | ### psql vs SQL commands 165 | 166 | `psql` has two different kinds of commands. Those starting with a backslash 167 | are for `psql` itself, as illustrated by the use of `\q` to quit. 168 | 169 | Those starting with valid SQL are of course interactive SQL used to 170 | create and modify PostgreSQL databases. 171 | 172 | ### Warning: SQL commands end with a semicolon! 173 | 174 | One gotcha is that almost all SQL commands you enter into `psql` must end in a semicolon. 175 | 176 | * For example,suppose you want to remove a table named `sample_property_5`. You'd enter this command: 177 | 178 | ```` 179 | postgres=# DROP TABLE "sample_property_5"; 180 | ```` 181 | 182 | It's easy to forget. If you do forget the semicolon, you'll see this perplexing prompt. 183 | Note that a `[` has been inserted before the username portion of the prompt, and another 184 | prompt appears below it: 185 | 186 | ```` 187 | [postgres=# DROP TABLE "sample_property_5" 188 | postgres=# 189 | 190 | ```` 191 | 192 | When you do, just remember to finish it off with that semicolon: 193 | 194 | ```` 195 | [postgres=# DROP TABLE "sample_property_5" 196 | postgres=# ; 197 | ```` 198 | 199 | ### Scrolling through the command history 200 | 201 | * Use the up and down arrow keys to move backwards and forwards through the command history. 202 | 203 | ## Getting information about databases 204 | 205 | These aren't SQL commands so just press Enter after them. Remember that: 206 | 207 | * When there's more output than fits the screen, it pauses. Press space to continue 208 | * If you want to halt the output, press `q`. 209 | 210 | ### \h Help 211 | 212 | ```` 213 | # Get help. Note it's a backslash, not a forward slash. 214 | postgres=# \h 215 | ```` 216 | You'll get a long list of commands, then output is paused: 217 | 218 | ```` 219 | Available help: 220 | ABORT CREATE USER 221 | ... 222 | ALTER AGGREGATE CREATE USER MAPPING 223 | ALTER PROCEDURE DROP INDEX 224 | : 225 | ```` 226 | 227 | * Press space to continue, or `q` to stop the output. 228 | 229 | You can get help on a particular item by listing it after the `\h` command. 230 | 231 | * For example, to get help on `DROP TABLE`: 232 | 233 | ````txt 234 | postgres=# \h drop table 235 | ```` 236 | 237 | You'll get help on just that item: 238 | 239 | ````txt 240 | Command: DROP TABLE 241 | Description: remove a table 242 | Syntax: 243 | DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ] 244 | ```` 245 | 246 | ### \l List databases 247 | 248 | What most people think of as a database (say, a list of customers) is actually a table. A database is a set of tables, information about those tables, information about users and their permissions, and much more. Some of these databases (and the tables within) are updated automatically by PostgreSQL as you use them. 249 | 250 | To get a list of all databases: 251 | 252 | ````txt 253 | postgres=# \l 254 | List of databases 255 | Name | Owner | Encoding | Collate | Ctype | Access privileges 256 | -----------+----------+----------+-------------+-------------+----------------------- 257 | visitor | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 258 | markets | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 259 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 260 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 261 | | | | | | postgres=CTc/postgres 262 | template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 263 | | | | | | postgres=CTc/postgres 264 | tom | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 265 | 266 | ```` 267 | 268 | You can get info on a single database by following the `\l` prompt with its name. 269 | 270 | * For example, to view information about the `template0` database: 271 | 272 | ````txt 273 | postgres=# \l template0 274 | ```` 275 | 276 | The output would be: 277 | 278 | ````txt 279 | postgres=# \l 280 | List of databases 281 | Name | Owner | Encoding | Collate | Ctype | Access privileges 282 | -----------+----------+----------+-------------+-------------+----------------------- 283 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 284 | | | | | | postgres=CTc/postgres 285 | ```` 286 | 287 | ### \l+ List databases with size, tablespace, and description 288 | 289 | To get additional information on the space consumed by database tables 290 | and comments describing those tables, use `\l+`: 291 | 292 | ```txt 293 | postgres=# \l+ 294 | ``` 295 | 296 | 297 | ### \x Expand/narrow table lists 298 | 299 | Use `\x` (X for eXpanded listing) to control 300 | whether table listings use a wide or narrow format. 301 | 302 | | Command | Effect | 303 | | ---------| -------------------------------------| 304 | | `\x off` | Show table listings in wide format | 305 | | `\x on` | Show table listings in narrow format | 306 | | `\x` | Reverse the previous state | 307 | | `\x auto`| Use terminal to determine format | 308 | 309 | **Example:** Here's an expanded listing: 310 | 311 | ```txt 312 | /* List all databases. */ 313 | postgres=# \l 314 | 315 | List of databases 316 | Name | Owner | Encoding | Collate | Ctype | Access privileges 317 | -----------+----------+----------+-------------+-------------+----------------------- 318 | foo | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 319 | foobarino | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 320 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 321 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 322 | | | | | | postgres=CTc/postgres 323 | template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 324 | | | | | | postgres=CTc/postgres 325 | tom | tom | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 326 | (6 rows) 327 | 328 | ``` 329 | 330 | Use `\x on` for narrower listings: 331 | 332 | ```txt 333 | /* Turn on narrow listings. */ 334 | postgres=# \x on 335 | postgres=# \l 336 | 337 | 338 | -[ RECORD 1 ]-----+---------------------- 339 | Name | foo 340 | Owner | tom 341 | Encoding | UTF8 342 | Collate | en_US.UTF-8 343 | Ctype | en_US.UTF-8 344 | Access privileges | 345 | -[ RECORD 2 ]-----+---------------------- 346 | Name | foobarino 347 | Owner | tom 348 | Encoding | UTF8 349 | Collate | en_US.UTF-8 350 | Ctype | en_US.UTF-8 351 | Access privileges | 352 | -[ RECORD 3 ]-----+---------------------- 353 | Name | postgres 354 | Owner | postgres 355 | Encoding | UTF8 356 | Collate | en_US.UTF-8 357 | Ctype | en_US.UTF-8 358 | Access privileges | 359 | 360 | ``` 361 | 362 | ### \c Connect to a database 363 | 364 | To see what's inside a database, connect to it using `\c` followed by the database name. 365 | The prompt changes to match the name of the database you're connecting to. 366 | (The one named `postgres` is always interesting.) Here we're connecting to the one named 367 | `markets`: 368 | 369 | ````txt 370 | postgres=# \c markets 371 | psql (11.1, server 11.0) 372 | You are now connected to database "markets" as user "tom". 373 | markets=# 374 | ```` 375 | 376 | ### \dt Display tables 377 | 378 | * Use `\dt` to list all the tables (technically, *relations*) in the database: 379 | 380 | ````txt 381 | markets=# \dt 382 | 383 | List of relations 384 | Schema | Name | Type | Owner 385 | --------+------------------------------+-------+---------- 386 | public | addresspool | table | tom 387 | public | adlookup | table | tom 388 | public | bidactivitysummary | table | tom 389 | public | bidactivitydetail | table | tom 390 | public | customerpaymentsummary | table | tom 391 | ... 392 | 393 | ```` 394 | 395 | * If you choose a database such as `postgres` there could be many tables. 396 | Remember you can pause output by pressing space or halt it by pressing `q`. 397 | 398 | ### \d and \d+ Display columns (field names) of a table 399 | 400 | To view the schema of a table, use `\d` followed by the name of the table. 401 | 402 | * To view the schema of a table named `customerpaymentsummary`, enter 403 | 404 | ``` 405 | markets=# \d customerpaymentsummary 406 | 407 | Table "public.customerpaymentsummary" 408 | Column | Type | Collation | Nullable | Default 409 | ------------------------------+-----------------------------+-----------+----------+-------- 410 | usersysid | integer | | not null | 411 | paymentattemptsall | integer | | | 412 | paymentattemptsmailin | integer | | | 413 | paymentattemptspaypal | integer | | | 414 | paymentattemptscreditcard | integer | | | 415 | paymentacceptedoutagecredit | integer | | | 416 | totalmoneyin | numeric(12,2) | | | 417 | updatewhen1 | timestamp without time zone | | | 418 | updatewhen2 | timestamp without time zone | | | 419 | 420 | ```` 421 | 422 | To view more detailed information on a table, use `\d+`: 423 | ````txt 424 | markets=# \d customerpaymentsummary 425 | 426 | Table "public.customerpaymentsummary" 427 | Column | Type | Collation | Nullable | Default | Storage | Stats target | 428 | ------------------------------+-----------------------------+-----------+----------+---------+---------+--------------- 429 | usersysid | integer | | not null | | plain | | 430 | paymentattemptsall | integer | | | | plain | | 431 | paymentattemptsmailin | integer | | | | plain | | 432 | paymentattemptspaypal | integer | | | | plain | | 433 | paymentattemptscreditcard | integer | | | | plain | | 434 | paymentacceptedoutagecredit | integer | | | | plain | | 435 | totalmoneyin | numeric(12,2) | | | | main | | 436 | updatewhen1 | timestamp without time zone | | | | plain | | 437 | updatewhen2 | timestamp without time zone | | | | plain | | 438 | 439 | Indexes: 440 | ```` 441 | 442 | ### \du Display user roles 443 | 444 | * To view all users and their roles, use `\du`: 445 | 446 | ````txt 447 | postgres=# \du 448 | List of roles 449 | Role name | Attributes | Member of 450 | ------------+------------------------------------------------------------+----------- 451 | smanager | Superuser | {} 452 | postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} 453 | tom | Superuser, Create role, Create DB | {} 454 | ```` 455 | 456 | * To view the role of a specific user, pass it after the `\du` command. 457 | For example, to see the only `tom` role: 458 | 459 | ````txt 460 | postgres=# \du tom 461 | List of roles 462 | Role name | Attributes | Member of 463 | ------------+------------------------------------------------------------+----------- 464 | tom | Superuser, Create role, Create DB | {} 465 | ```` 466 | 467 | ## Creating a database 468 | 469 | Before you add tables, you need to create a database to contain those tables. 470 | That's not done with `psql`, but instead it's done with `createdb` 471 | (a separate external command; see the PostgreSQL [createdb](https://www.postgresql.org/docs/current/app-createdb.html) documentation) at the operating system command line: 472 | 473 | ````bash 474 | # Replace markets with your database name 475 | $ createdb markets 476 | ```` 477 | On success, there is no visual feedback. Thanks, PostgreSQL. 478 | 479 | ## Adding tables and records 480 | 481 | ### Creating a table (CREATE TABLE) 482 | 483 | To add a table schema to the database: 484 | 485 | ````sql 486 | postgres=# create table if not exists product ( 487 | id SERIAL, 488 | name VARCHAR(100) NOT NULL, 489 | sku CHAR(8) 490 | ); 491 | ```` 492 | 493 | And `psql` responds with: 494 | 495 | ````sql 496 | CREATE TABLE 497 | ```` 498 | 499 | For more see `CREATE TABLE` in the [PostgreSQL official docs](https://www.postgresql.org/docs/10/sql-createtable.html). 500 | 501 | ### Adding a record (INSERT INTO) 502 | 503 | * Here's how to add a record, populating every field: 504 | 505 | ````bash 506 | # The id field is an automatically assigned 507 | # when you use DEFAULT. The serial primary key means 508 | # that number will be increased by at least 509 | # 1 and assigned to that same field when 510 | # a new record is created. 511 | # Using DEFAULT is a placeholder. 512 | # In its place PostgreSQL automatically generates a unique integer for it. 513 | postgres=# INSERT INTO product VALUES(DEFAULT, 'Apple, Fuji', '4131'); 514 | ```` 515 | 516 | PostgreSQL responds with: 517 | 518 | ```sql 519 | INSERT 0 1 520 | ``` 521 | 522 | * Try it again and you get a simliar response. 523 | 524 | ````sql 525 | postgres=# INSERT INTO product VALUES(DEFAULT, 'Banana', '4011'); 526 | INSERT 0 1 527 | ```` 528 | 529 | ### Adding (inserting) several records at once 530 | 531 | * You can enter a list of records using this syntax: 532 | 533 | ```` 534 | postgres=# INSERT INTO product VALUES 535 | (DEFAULT, 'Carrots', 4562), 536 | (DEFAULT, 'Durian', 5228) 537 | ; 538 | ```` 539 | 540 | 541 | #### Adding only specific (columns) fields from a record 542 | 543 | You can add records but specify only selected fields (also known as columns). MySQL will use common sense default values for the rest. 544 | 545 | In this example, only the `name` field will be populated. The `sku` column is left blank, and the `id` column is incremented and inserted. 546 | 547 | Two records are added: 548 | 549 | ```sql 550 | postgres=# INSERT INTO product (name) VALUES 551 | ('Endive'), 552 | ('Figs') 553 | ; 554 | ``` 555 | 556 | PostgreSQL responds with the number of records inserted: 557 | 558 | ````txt 559 | INSERT 0 2 560 | ```` 561 | 562 | For more on INSERT, see `INSERT` in the [PostgreSQL official docs](https://www.postgresql.org/docs/current/sql-insert.html) 563 | 564 | 565 | ### Doing a simple query--get a list of records (SELECT) 566 | 567 | Probably the most common thing you'll do with a table is to obtain information about it 568 | with the `SELECT` statement. It's a [huge topic](https://www.postgresql.org/docs/current/sql-select.html) 569 | 570 | 571 | * Let's list all the records in the `product` table: 572 | 573 | ```sql 574 | postgres=# SELECT * FROM product; 575 | ``` 576 | 577 | The response: 578 | 579 | ```txt 580 | postgres=# select * from product; 581 | id | name | sku 582 | ----+-------------+---------- 583 | 1 | Apple, Fuji | 4131 584 | 2 | Banana | 4011 585 | (2 rows) 586 | ``` 587 | 588 | #### Note 589 | 590 | If your table has mixed case objects such as column names or indexes, you'll need to enclose them in double quotes. 591 | For example, If a column name were `Product` instead of `product` your query would need to look like this: 592 | 593 | ````sql 594 | SELECT * FROM "product"; 595 | ```` 596 | For more on SELECT, see the `SELECT` in the [PostgreSQL official docs](https://www.postgresql.org/docs/current/sql-select.html). 597 | 598 | 599 | 600 | ## Maintenance and operations issues 601 | 602 | ## Timing 603 | 604 | 605 | ### \t Timing SQL operations 606 | 607 | Use `\t` to show timing for all SQL operations performed. 608 | 609 | | Command | Effect | 610 | | --------------| -------------------------------------| 611 | | `\timing off` | Disable timing of SQL operations | 612 | | `\timing on` | Show timing after all SQL operations | 613 | | `\timing` | Toggle (reverse) the setting | 614 | 615 | ### Example of \t Timing command 616 | 617 | ``` 618 | tom=# insert into todo values ('Retry on Android before app submission,'8.x and earlier'); 619 | INSERT 0 1 620 | tom=# \timing on 621 | Timing is on. 622 | tom=# insert into todo values ('Correct footer bug','Mobile version only'); 623 | INSERT 0 1 624 | Time: 1.067 ms 625 | tom=# insert into todo values ('Retry on Android before app submission', '8.x and earlier'); 626 | INSERT 0 1 627 | Time: 23.312 ms 628 | tom=# \timing 629 | Timing is off. 630 | ``` 631 | 632 | ## Watch 633 | 634 | The `\watch` command repeats the previous command at the specified interval. 635 | To use it, enter the SQL command you want repeated, then 636 | use `\watch` followed by the number of seconds you want for 637 | the interval between repeats, for rexample, `\watch 1` 638 | to repeat it every second. 639 | 640 | ### Example of the \Watch command 641 | 642 | Here's an example of using `\watch` to see if any records have been 643 | inserted within the last 5 seconds. 644 | 645 | ``` 646 | tom=# select count(*); 647 | count 648 | -------- 649 | 726 650 | (726 rows) 651 | 652 | tom=# \watch 5 653 | Mon Nov 16 13:50:36 2020 (every 2s) 654 | 655 | count 656 | -------- 657 | 726 658 | (726 rows) 659 | 660 | Mon Nov 16 13:50:38 2020 (every 2s) 661 | 662 | count 663 | -------- 664 | 726 665 | (726 rows) 666 | 667 | Mon Nov 16 13:50:40 2020 (every 2s) 668 | 669 | count 670 | -------- 671 | 726 672 | (726 rows) 673 | 674 | ``` 675 | 676 | ### Locate the pg_hba.conf file 677 | 678 | Postgres configuration is stored in a file named `pg_hba.conf` *somewhere* in the file system, but 679 | that location varies widely. The way to find it is to use `show hba_file` like this: 680 | 681 | ```sql 682 | show hba_file; 683 | ``` 684 | 685 | See below for hot reloading this file while Postgres is running. 686 | 687 | ### Reload the configuration file while Postgres is running 688 | 689 | If you make changes to the `pg_hba.conf` Postgres configuration sometimes you need to restart. 690 | But you may just choose to reload the `pg_hba.conf` configuration file like this: 691 | 692 | ```sql 693 | SELECT pg_reload_conf(); 694 | ``` 695 | 696 | ## Reference 697 | 698 | * PostgreSQL offical docs: [Server Administration](https://www.postgresql.org/docs/current/admin.html) 699 | * `psql` , a.k.a the [PostgreSQL interactive terminal](https://www.postgresql.org/docs/current/app-psql.html) 700 | * `createdb` in the [PostgreSQL offical docs](https://www.postgresql.org/docs/current/app-createdb.html) 701 | * `CREATE TABLE` in the [PostgreSQL official docs](https://www.postgresql.org/docs/current/sql-createtable.html) 702 | * `INSERT` in the [PostgreSQL official docs](https://www.postgresql.org/docs/current/sql-insert.html) 703 | 704 | 705 | 706 | 707 | --------------------------------------------------------------------------------