-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CrunchyData/master
Re-sync Crunchy
- Loading branch information
Showing
23 changed files
with
356 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
pg-administration/basic-postgresql-for-dbas/pgbackrest/index.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
pg-administration/basic-postgresql-for-dbas/pgbackrest/intro.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# PGBackRest | ||
This scenario will go over the basics of using pgbackrest to perform backups and restoration. It has many, many more options available within it for customizing your backup processes, but this scenario should help you get started with it. | ||
# pgBackRest | ||
This scenario will go over the basics of using pgBackRest to perform backups and restoration. It has many, many more options available within it for customizing your backup processes, but this scenario should help you get started with it. | ||
|
||
Full documentation is available at https://pgbackrest.org/ | ||
|
||
Let's get started! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Introduction to `psql` | ||
|
||
`psql` is described in the [docs](https://www.postgresql.org/docs/12/app-psql.html) as a "terminal-based front-end to PostgreSQL." This means that instead of having to use a GUI tool such as [pgAdmin](https://www.pgadmin.org/) to create, access, or manipulate a database, you can use text commands via a command line interface (CLI). | ||
|
||
We'll get started by connecting to a database. | ||
|
||
### Connect to the `workshop` database | ||
|
||
The database contains storm events data, which is public domain data from the National Weather Service. | ||
|
||
In your interactive terminal, you're currently logged in as root user, as indicated by the command line prompt: | ||
|
||
![Terminal prompt](./assets/01_-_command_line_prompt.PNG) | ||
|
||
> **Note** | ||
> | ||
> Depending on the system you're using and the shell (i.e. program you're in), | ||
> the prompt might look a little different. You'll see this below when you're | ||
> in the `psql` shell. | ||
We want to connect to the PostgreSQL database as **groot**, so we'll enter the `psql` command, accompanied by some flags: | ||
|
||
``` | ||
psql -h localhost -U groot -d workshop | ||
```{{execute}} | ||
Each command line flag or option begins with a `-` or `--`, and what follows after the space is the parameter you're passing in with the command. | ||
1. The `-h` option specifies the host server (e.g. `localhost`), or socket directory. | ||
2. `-U` specifies the user you're connecting as (`groot`). | ||
3. `-d` specifies the database you're connecting to (`workshop`). | ||
The CLI shows a response whenever a command is run. In this case, since you are logging in to a database as the user **groot**, the command line will then prompt you for the password (enter `password`). You won't see the password displayed as you type, but the terminal is recording your keystrokes. | ||
![Terminal prompt for password](./assets/01_-_password_prompt.PNG) | ||
Once you're logged in to the database server, the prompt in the terminal should change. The psql prompt indicates the database you're connected to, along with `=>`. | ||
![Postgres prompt](01_-_postgres_prompt.PNG) | ||
The beauty of the command line is that there are often shortcuts or tips and tricks. For example, you could shorten the command used above even further to: | ||
`psql -h localhost workshop groot` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
## Meta-commands | ||
|
||
The `psql` shell comes with meta-commands, which begin with an unquoted backslash. | ||
|
||
### Get help | ||
|
||
An example of one is `\?`, which will bring up help information about commands available in `psql`. | ||
|
||
`\?`{{execute}} | ||
|
||
> **Tip** | ||
> Press the spacebar to scroll through the list, or press `q` any time to return > to the `psql` prompt. | ||
You should see the list of available meta-commands, including the one you just used. You'll see that the `\?` command can also include parameters that allow you to look up specific topics, e.g. `\? options` will bring up help information on psql options (flags). | ||
|
||
The `\h` command will bring up a list of SQL commands. | ||
|
||
`\h`{{execute}} | ||
|
||
And if you add the name of a command, you'll be shown more detailed information: | ||
|
||
`\h ALTER INDEX`{{execute}} | ||
|
||
### Describe a table | ||
|
||
`\d` with the name of a table will display table metadata, e.g. columns, data types, and other attributes. | ||
|
||
`\d se_details`{{execute}} | ||
|
||
`\d` used without any parameters will show a list of all available tables in the current database. If `+` is appended, you'll also see extended information such as each table's size on disk. | ||
|
||
`\d+`{{execute}} | ||
|
||
Give it a try with a table name as well, and see the difference: | ||
|
||
`\d+ se_details`{{execute}} | ||
|
||
There are more in the `\d` set of meta-commands that you can use. Examples of more common ones are `\dn` (all schemas), `\dv` (all available views), `\du` (all users), `\df` (all functions). | ||
|
||
## Options | ||
|
||
As you saw in the earlier step, the `psql` command can be run with options. | ||
|
||
One useful option is `--help`, which displays help information about `psql`, and then exits (i.e. you don't stay in the `psql` shell). | ||
|
||
Let's log out of PostgreSQL first (by entering `\q`, or typing `CTRL`+`d`), and then run `psql` again with `--help` this time. | ||
|
||
`\q`{{execute}} | ||
|
||
`psql --help`{{execute}} | ||
|
||
<!--- | ||
psql -l | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Running queries in `psql` | ||
|
||
Once you're in the `psql` shell (i.e. logged in), you can run SQL queries. | ||
|
||
Let's connect to the database again: | ||
|
||
``` | ||
psql -h localhost -U groot -d workshop | ||
```{{execute}} | ||
``` | ||
SELECT cz_name FROM se_details LIMIT 5; | ||
```{{execute}} | ||
Note that SQL statements need to end in a semicolon. The `psql` command or other command-line statements, however, do not. | ||
### Multiline queries | ||
You can press `ENTER` in the `psql` shell if you want the SQL statement to continue on a new line -- `psql` won't process the statement until you add the semicolon at the end of a line. | ||
![Multiline query in terminal](./assets/03_-_multiline.PNG) | ||
You'll see that the command line prompt starts with a `=>`, but changes to a `->` if the statement continues on to the next line (i.e. if you press enter without ending with a `;`). The prompt doesn't change back to `=>` until the statement ends and runs. |
Oops, something went wrong.