|
| 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 | +``` |
0 commit comments