sudo apt install -y postgresql
[user@localhost ~]$ sudo netstat -tlnp | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1761/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 1761/postgres
sudo -u postgres psqlHere "-u postgres" means "run command (or edit file) as specified user name or ID". Above command open psql as postgres user.
postgres=# CREATE DATABASE my_database;To create a database, you must be a superuser or have the special CREATEDB privilege.
postgres=# DROP DATABASE my_database;DROP DATABASE drops a database. It removes the catalog entries for the database and deletes the directory containing the data. It can only be executed by the database owner. It cannot be executed while you are connected to the target database. (Connect to postgres or any other database to issue this command.) Also, if anyone else is connected to the target database, this command will fail unless you use the FORCE option described below.
postgres=# CREATE USER myuser WITH PASSWORD 'password';CREATE USER adds a new user to a PostgreSQL database cluster. You must be a database superuser to use this command. Documentation.
postgres=# ALTER USER myuser WITH PASSWORD 'new_password';ALTER USER changes the attributes of a PostgreSQL user account. Attributes not mentioned in the command retain their previous settings.
postgres=# DROPUSER newuser;DROPUSER removes an existing PostgreSQL user. Only superusers and users with the CREATEROLE privilege can remove PostgreSQL users. (To remove a superuser, you must yourself be a superuser.)
postgres=# GRANT ALL PRIVILEGES ON DATABASE my_database TO myuser;The GRANT command has two basic variants: one that grants privileges on a database object (table, column, view, foreign table, sequence, database, foreign-data wrapper, foreign server, function, procedure, procedural language, large object, configuration parameter, schema, tablespace, or type), and one that grants membership in a role. These variants are similar in many ways, but they are different enough to be described separately. Documentation.
postgres=# SELECT * FROM users;
postgres=# SELECT * FROM users WHERE username = 'admin';
postgres=# INSERT INTO users (username, email) VALUES ('johndoe', 'johndoe@example.com');
postgres=# UPDATE users SET column1 = value1, column2 = value2 WHERE condition;
postgres=# DELETE FROM users;
postgres=# DELETE FROM users WHERE last_login < NOW() - INTERVAL '30 days';
postgres=# \q
pg_dump dbname > dumpfile
sudo -u postgres pg_dump my_database > my_database.sql
sudo -u postgres pg_dump -d my_database --table my_table > my_table.sql
psql empty_database < my_table.sql
listen_addresses = '0.0.0.0/0' # what IP address(es) to listen on;
host all all 0.0.0.0/0 scram-sha-256
sudo systemctl restart postgresql