-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Restructured docs * Reworked the What's in it section * Added power number * Added icons
- Loading branch information
1 parent
554dd61
commit 2895c65
Showing
34 changed files
with
573 additions
and
265 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Quickstart" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Connect to the PostgreSQL server | ||
|
||
With PostgreSQL server up and running, let's connect to it. | ||
|
||
By default, the `postgres` user and the `postgres` database are created in PostgreSQL upon its installation and initialization. This allows you to connect to the database as the `postgres` user. | ||
{.power-number} | ||
|
||
1. Switch to the `postgres` user. | ||
|
||
```{.bash data-prompt="$"} | ||
$ sudo su postgres | ||
``` | ||
|
||
2. Open the PostgreSQL interactive terminal `psql`: | ||
|
||
```{.bash data-prompt="$"} | ||
$ psql | ||
``` | ||
|
||
<i info>:material-information: Hint:</i> You can connect to `psql` as the `postgres` user in one go: | ||
|
||
```{.bash data-prompt="$"} | ||
$ sudo su - postgres -c psql | ||
``` | ||
|
||
|
||
## Basic `psql` commands | ||
|
||
While connected to PostgreSQL, let's practice some basic `psql` commands to interact with the database: | ||
1. List databases: | ||
```{.bash data-prompt="$"} | ||
$ \l | ||
``` | ||
2. Display tables in the current database: | ||
```{.bash data-prompt="$"} | ||
$ \dt | ||
``` | ||
3. Display columns in a table | ||
```{.bash data-prompt="$"} | ||
$ \d <table_name> | ||
``` | ||
4. Switch databases | ||
```{.bash data-prompt="$"} | ||
$ \c <database_name> | ||
``` | ||
5. Display users and roles | ||
```{.bash data-prompt="$"} | ||
$ \du | ||
``` | ||
6. Exit the `psql` terminal: | ||
```{.bash data-prompt="$"} | ||
$ \q | ||
``` | ||
To learn more about using `psql`, see [`psql` :octicons-link-external-16:](https://www.postgresql.org/docs/current/app-psql.html) documentation. | ||
Congratulations! You have connected to PostgreSQL and learned some essential `psql` commands. | ||
## Next steps | ||
[Manipulate data in PostgreSQL :material-arrow-right:](crud.md){.md-button} |
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,112 @@ | ||
# Manipulate data in PostgreSQL | ||
|
||
On the previous step, you have [connected to PostgreSQL](connect.md) as the superuser `postgres`. Now, let's insert some sample data and operate with it in PostgreSQL. | ||
|
||
## Create a database | ||
|
||
Let's create the database `test`. Use the CREATE DATABASE command: | ||
|
||
```sql | ||
CREATE DATABASE test; | ||
``` | ||
|
||
## Create a table | ||
|
||
Let's create a sample table `Customers` in the `test` database using the following command: | ||
|
||
```sql | ||
CREATE TABLE customers ( | ||
id SERIAL PRIMARY KEY, -- 'id' is an auto-incrementing integer | ||
first_name VARCHAR(50), -- 'first_name' is a string with a maximum length of 50 characters | ||
last_name VARCHAR(50), -- 'last_name' is a string with a maximum length of 50 characters | ||
email VARCHAR(100) -- 'email' is a string with a maximum length of 100 characters | ||
); | ||
``` | ||
|
||
<i info>:material-information: Hint:</i>Having issues with table creation? Check our [Troubleshooting guide](troubleshooting.md) | ||
|
||
## Insert the data | ||
|
||
Populate the table with the sample data as follows: | ||
|
||
```sql | ||
INSERT INTO customers (first_name, last_name, email) | ||
VALUES | ||
('John', 'Doe', '[email protected]'), -- Insert a new row | ||
('Jane', 'Doe', '[email protected]'); | ||
('Alice', 'Smith', '[email protected]'); | ||
``` | ||
|
||
## Query data | ||
|
||
Let's verify the data insertion by querying it: | ||
|
||
```sql | ||
SELECT * FROM customers; | ||
``` | ||
|
||
??? example "Expected output" | ||
|
||
```{.sql .no-copy} | ||
id | first_name | last_name | email | ||
----+------------+-----------+------------------------- | ||
1 | John | Doe | [email protected] | ||
2 | Jane | Doe | [email protected] | ||
3 | Alice | Smith | [email protected] | ||
(3 rows) | ||
``` | ||
|
||
## Update data | ||
|
||
Let's update John Doe's record with a new email address. | ||
|
||
1. Use the UPDATE command for that: | ||
|
||
```sql | ||
UPDATE customers | ||
SET email = '[email protected]' | ||
WHERE first_name = 'John' AND last_name = 'Doe'; | ||
``` | ||
|
||
2. Query the table to verify the updated data: | ||
|
||
```sql | ||
SELECT * FROM customers WHERE first_name = 'John' AND last_name = 'Doe'; | ||
``` | ||
|
||
??? example "Expected output" | ||
|
||
```{.sql .no-copy} | ||
id | first_name | last_name | email | ||
----+------------+-----------+------------------------- | ||
2 | Jane | Doe | [email protected] | ||
3 | Alice | Smith | [email protected] | ||
1 | John | Doe | [email protected] | ||
(3 rows) | ||
``` | ||
|
||
## Delete data | ||
|
||
Use the DELETE command to delete rows. For example, delete the record of Alice Smith: | ||
|
||
```sql | ||
DELETE FROM Customers WHERE first_name = 'Alice' AND last_name = 'Smith'; | ||
``` | ||
|
||
If you wish to delete the whole table, use the `DROP TABLE` command instead as follows: | ||
|
||
```sql | ||
DROP TABLE customers; | ||
``` | ||
|
||
To delete the whole database, use the DROP DATABASE command: | ||
|
||
```sql | ||
DROP DATABASE test; | ||
``` | ||
|
||
Congratulations! You have used basic create, read, update and delete (CRUD) operations to manipulate data in Percona Distribution for PostgreSQL. To deepen your knowledge, see the [data manipulation :octicons-link-external-16:](https://www.postgresql.org/docs/{{pgversion}}/dml.html) section in PostgreSQL documentation. | ||
|
||
## Next steps | ||
|
||
[What's next?](whats-next.md)(.md-button) |
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
Oops, something went wrong.