setup/doc/postgres-create-user.md

61 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2022-05-25 13:25:51 +02:00
# Create User with Postgres
2023-11-16 10:27:45 +01:00
* create a database user called `user name` with password `key`
2022-05-25 13:25:51 +02:00
```
2024-02-21 13:28:20 +01:00
CREATE USER <user name> with encrypted password 'key';
```
* create a `SUPERUSER` user called `user name` with password `key`
```
2024-03-11 15:43:27 +01:00
CREATE USER <user name> with SUPERUSER encrypted password 'key';
```
2023-11-16 10:27:45 +01:00
* OPTION: gant user `user name` access to database `database name`
```
2022-10-09 09:40:27 +02:00
GRANT ALL PRIVILEGES ON DATABASE <database name> TO <user name>;
2022-05-25 13:25:51 +02:00
```
2023-11-16 10:27:45 +01:00
* OPTION: add all privileges for a `schema name` to a `user`
2022-09-16 10:15:55 +02:00
```
2022-10-09 09:40:27 +02:00
GRANT ALL PRIVILEGES ON ALL TABALES ON SCHEMA <schema name> TO <user name>;
2022-09-16 10:15:55 +02:00
```
2023-11-16 10:27:45 +01:00
* OPTION: if `user name` already exist, pw can be changed this way
2022-09-09 11:47:46 +02:00
```
ALTER USER <user name> WITH ENCRYPTED PASSWORD '<key>';
```
2023-11-16 10:27:45 +01:00
* OPTION: gant user `user name` access to database `postgres` so that this user is able to login without providing a database
2022-09-05 11:51:14 +02:00
```
sudo -u postgres psql -W
GRANT ALL PRIVILEGES ON DATABASE postgres to <user name>;
2022-09-05 11:51:14 +02:00
```
2023-11-16 10:27:45 +01:00
* OPTION: gant user `user name` permission to create databases
2022-09-05 11:51:14 +02:00
```
sudo -u postgres psql -W
2023-11-16 10:27:45 +01:00
ALTER USER <user name> CREATEDB;
2022-09-05 11:51:14 +02:00
```
2022-05-25 13:25:51 +02:00
* edit the pg_hba.conf file\
NOTE: Insert the postgresql database version that is running on the respective host
```
sudo cp /etc/postgresql/<psql version>/main/pg_hba.conf /etc/postgresql/<psgl version>/main/pg_hba.conf-backup
sudo vi /etc/postgresql/<psql version>/main/pg_hba.conf
```
* in order to be able to run applications like e.g. Spring Boot with a local PostgreSQL installation, change the authentication method for the Unix domain socket and local connections to trust like this
```
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
```
* restart PostgreSQL to enable the changes
```
systemctl status postgresql
sudo systemctl restart postgresql
systemctl status postgresql
```