Skip to content

Commit 5461a52

Browse files
committed
Doc (style): Update the structure of files in postgres articles
1 parent 24a2beb commit 5461a52

File tree

6 files changed

+242
-211
lines changed

6 files changed

+242
-211
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Database Management Tools
2+
3+
These are software applications that help users to manage SQL server infrastructure. They allow users to configure, monitor, manage and administer SQL servers and databases. As a web developer or as a database administrator, you will be dealing with SQL statements to explore the database for various reasons:
4+
5+
- Querying the database
6+
- Build and execute SQL code
7+
- Generating reports
8+
- Making a backup
9+
- Diagonising an application for database-related problems
10+
11+
It is therefore important to choose and have the right tool that can speed up database-related tasks and make you more productive. There are many tools available in the market that can be used to work with a database. To name two, we have [pgAdmin](https://www.pgadmin.org/) and [DBeaver](https://dbeaver.io). I will use DBeaver throughout the subsquent articles as we strive to understand SQL in-depth.
12+
13+
There are the two ways you can connect to PostreSQL:
14+
15+
1. [Using `psql`](databases/access_postgresql/psql.md)
16+
2. [Using SQL client software application and database administration tool](databases/access_postgresql/dbeaver.md) (this article)

databases/access_postgresql/psql.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Psql
2+
3+
## Overview
4+
5+
**Psql** is a terminal-based front-end for PostgreSQL. It allows you to issue queries interactively. Once you have connected to the PostgreSQL server, you can begin querying immediately. Besides basic querries, you can also issue certain commands such as `\d` to list all tables in the database, `\c dbname` to connect to another database and `\q` to quit from the postgres shell. You can access a text editor inside `psql` using `\e`. Run `\?` to see what other commands are available for use.
6+
7+
There are the two ways you can connect to PostreSQL:
8+
9+
1. [Using `psql`](databases/access_postgresql/psql.md) (this article)
10+
2. [Using SQL client software application and database administration tool](databases/access_postgresql/dbeaver.md)
11+
12+
13+
## Connecting to the PostgreSQL Server Using `psql`
14+
15+
Now that you have installed postgreSQL, that first thing you want to do is to connect to its server. Every installation creates a default user called `postgres` who is associated with the default Postgres role. We will begin by connecting to the server using this user.
16+
17+
```python
18+
$ psql
19+
```
20+
21+
You may notice that running the command above produces an error:
22+
23+
```python
24+
# Ouput
25+
26+
psql: error: connection to the server on socket "/var/run/posgresql..." failed: No such file or directory. Is the server running locally and accepting connections on the socket?
27+
```
28+
29+
What this means is that our server is currently not running. To start it, we will run:
30+
31+
```python
32+
$ sudo service postgresql restart
33+
34+
# Output
35+
* Restarting PostgreSQL 14 database server
36+
```
37+
38+
With the connection created, we can now access the `psql` command to access the interactive terminal for PostgreSQL. This command is normally used in conjunction with a user.
39+
40+
```python
41+
$ sudo -u postgres psql
42+
```
43+
44+
We are using the default `postgres` user to access PostgreSQL terminal. You will be asked to provide a password to continue since you are using `sudo` command. When you do so, your terminal will change to this:
45+
46+
```python
47+
psql (14.4 (Ubuntu 14.4-1.pgdg20.04+1))
48+
Type "help" for help.
49+
50+
postgres=#
51+
```
52+
53+
## Create Another User
54+
55+
At this stage, we are ready to start issuing SQL commands. The first action we will take is to create another user. We will use SQL statements to accomplish this. If you are not familiar with SQL commands, do not worry. The subsequent chapters are dedicated to help you understand SQL.
56+
57+
```python
58+
postgres=# CREATE USER muthoni;
59+
CREATE ROLE
60+
```
61+
62+
The CREATE USER query is an alias (which means "an assumed identity") of CREATE ROLE <name> WITH LOGIN; If you are curious how many users you have in the database, you can run this command:
63+
64+
```python
65+
postgres=# \du
66+
67+
# Output
68+
Role name | List of roles Attributes | Member of
69+
-------------+--------------------------------------------------------------------------+-----------
70+
muthoni | | {}
71+
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
72+
```
73+
74+
From a security stanpoint, it is very dangerous to have a user with superuser privileges because this user is able to bypass all checks. You are advised to avoid the superuser unless it is necessary or be VERY careful.
75+
76+
## Create a Super User
77+
78+
Let us create another user with superuser privileges:
79+
80+
```python
81+
postgres=# CREATE USER wangare SUPERUSER;
82+
CREATE ROLE
83+
84+
# Output
85+
Role name | List of roles Attributes | Member of
86+
-------------+--------------------------------------------------------------------------+-----------
87+
muthoni | | {}
88+
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
89+
wangare | Superuser | {}
90+
```
91+
92+
The query above is the same as `CREATE ROLE <name> LOGIN SUPERUSER;`.
93+
94+
Alternatively, you can simple create a superuser from the default Ubuntu terminal:
95+
96+
```python
97+
$ sudo -u postgres createuser --superuser <name>
98+
```
99+
100+
## Create User Password
101+
102+
With the uses in place, the next step would be to create passwords for them. This is to ensure that only them, and those privy to the credential, will be able to access the database. From `psql`, we can do this:
103+
104+
```python
105+
postgres=# CREATE USER chico WITH PASSWORD 'hard-to-guess';
106+
CREATE ROLE
107+
```
108+
109+
If the user already exists, we can the ALTER command:
110+
111+
```python
112+
postgres=# ALTER USER muthoni WITH PASSWORD 'difficult-password';
113+
ALTER ROLE
114+
```
115+
116+
## Change User Password
117+
118+
You may forget your user password, or you would simple want to modify it as a good security practice. Let us do this:
119+
120+
```python
121+
$ sudo -u postgres psql
122+
psql (14.4 (Ubuntu 14.4-1.pgdg20.04+1))
123+
Type "help" for help.
124+
125+
postgres=#\password muthoni
126+
Enter new password for user "muthoni":
127+
Enter it again:
128+
postgres=#
129+
```
130+
131+
Or you may do everything from the default Ubuntu terminal as follows:
132+
133+
```python
134+
$ sudo - postgres psql -c "ALTER USER muthoni PASSWORD 'new-password';"
135+
```
136+
137+
You can then restart your PostgreSQL server:
138+
139+
```python
140+
$ sudo service postgresql restart
141+
```

databases/install_postgresql.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Install PostgreSQL
2+
3+
Now that you have a brief understanding of what a database is, you know the history of PostgreSQL, and are familiar with some of the advantages of using PostgreSQL, you are ready to start using it. We will begin by learning how to install it in our operating system. We will also be using the DBeaver client software to help us get off the ground quickly.
4+
5+
For reference, these are the topics we will cover in this tutorial:
6+
7+
- [PostgreSQL Overview](postgresql.md)
8+
- [Install PostgreSQL and DBeaver](install_postgresql_and_dbeaver.md) (this article)
9+
- [Getting Started with PostgreSQL](getting_started_with_postgresql.md)
10+
11+
### Table of Contents
12+
13+
In this article, we will focus on installing PostgreSQL, creating a connection to it and optionally know how to uninstall it.
14+
15+
1. [Install PostgreSQL in Linux Using the Terminal](#install-postgresql-in-linux-using-the-terminal)
16+
2. [Create Connection to PostgreSQL](#create-connection-to-postgresql)
17+
3. [Uninstall PostgreSQL in Linux Using the Terminal](#uninstall-postgresql-in-linux-using-the-terminal)
18+
19+
20+
## Install PostgreSQL in Linux Using the Terminal
21+
22+
Most Linux distributions such as Debian and Ubuntu have PostgreSQL integrated with their package environments by default. However, Ubuntu "snapshots" a specific version of PostgreSQL that is then supported throughout the lifetime of that Ubuntu version. Other versions of PostgreSQL are available through the PostgreSQL apt repository. It is recommended that you install PostgreSQL this way since it ensures a proper integration with the operating system, including automatic patching and other management functionality.
23+
24+
If you try to search for an available version of PostgreSQL after a fresh install of Ubuntu by running `psql -V`, you will notice that the command `psql` will not be found. You will be told that it can be installed by running:
25+
26+
```python
27+
sudo apt install postgresql-client-common
28+
```
29+
30+
![No postresql](/images/databases/postgresql/no_postgresql.png)
31+
32+
When installed, running `psql -V` would show this outcome:
33+
34+
```python
35+
psql (PostgreSQL) 14.4 (Ubuntu 14.4-1.pgdg20.04+1)
36+
```
37+
38+
Install and build from source by visiting the [Linux downloads (Ubuntu)](https://www.postgresql.org/download/linux/ubuntu/) for more information.
39+
40+
41+
## Create Connection to PostgreSQL
42+
43+
User privilege and privilege access management (PAM) is a serious security concept when working with databases. What privilege access does is to empower organizations to reduce the threat that may come due to data breaches. For example, a business might have two employees with administrator access to a database. Only these users have the authority to work with the database and can delete or setup objects in a database.
44+
45+
From the [PostgreSQL Overview](postgresql.md) article, you may have noticed that one of the advantages of using PostgreSQL is its ability to handle access control through roles and privileges.
46+
47+
There are two ways we can access PostgreSQL. Check them using the links below:
48+
49+
1. [Using `psql`](databases/access_postgresql/psql.md)
50+
2. [Using SQL client software application and database administration tool](databases/access_postgresql/dbeaver.md)
51+
52+
You can choose one over the other based on your personal preferrance, but it is good to know of both ways.
53+
54+
55+
## Uninstall PostgreSQL in Linux Using the Terminal
56+
57+
For whatever reason, if you wish to uninstall PostgreSQL from your system, you can do the following:
58+
59+
```python
60+
$ dpkg -l | grep postgres
61+
62+
# Output
63+
ii pgdg-keyring 2018.2 all keyring for apt.postgresql.org
64+
ii postgresql 14+242.pgdg20.04+1 all object-relational SQL database (supported version)
65+
ii postgresql-14 14.4-1.pgdg20.04+1 amd64 The World's Most Advanced Open Source Relational Database
66+
ii postgresql-client-14 14.4-1.pgdg20.04+1 amd64 front-end programs for PostgreSQL 14
67+
ii postgresql-client-common 241.pgdg20.04+1 all manager for multiple PostgreSQL client versions
68+
ii postgresql-common 241.pgdg20.04+1 all PostgreSQL database-cluster manager
69+
```
70+
71+
The command above will list all dependant packages of postgres (or postgresql). Then, you can uninstall these packages individually:
72+
73+
```python
74+
$ sudo apt --purge remove package_name package_name
75+
```
76+
77+
`purge` completely erases traces of the configuration files of a program from the system. It is particularly useful when you want to "start all over again" with an application because you messed up the configuration. See this discussion on [StackOverfolow](https://askubuntu.com/a/187891/789542).
78+
79+
Now that everything has been uninstalled, you can verify the uninstallation process by running:
80+
81+
```python
82+
$ psql -V
83+
```

0 commit comments

Comments
 (0)