هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
PostgreSQL is the most used open-source database in cloud-native apps (Azure Database for PostgreSQL, AWS RDS, CloudNativePG on Kubernetes). psql connects to a server (port 5432), runs SQL and has meta-commands that start with a backslash (\l, \dt, \du). pg_dump makes logical backups of one database; pg_dumpall dumps roles and all databases.
On RHEL: dnf install postgresql-server, then postgresql-setup --initdb. Access rules are in pg_hba.conf.
طريقة الكتابة
psql [-h HOST] [-p PORT] [-U USER] [-d DB] [-c SQL | -f FILE]
pg_dump -Fc -d DB -f FILE.dumpالأجزاء الرئيسية
psql meta-commands
\l- List databases.
\cDB- Connect to another database.
\dt/\dTABLE- List tables / describe a table.
\du- List roles.
\dn/\df/\dv- Schemas / functions / views.
\x- Expanded output (one field per line).
\timing- Show query time.
\iFILE- Run a SQL file.
\q- Quit.
\conninfo- Show the current connection.
الخيارات 8
الأكثر استخدامًا
Flags
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 10
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Install and start on RHEL
2Create an app role and database (least privilege)
sudo -u postgres psql postgres=# CREATE ROLE shop LOGIN PASSWORD 'Ch4nge-me-2026'; postgres=# CREATE DATABASE shop OWNER shop; postgres=# REVOKE ALL ON DATABASE shop FROM PUBLIC;3Connect with TLS
psql "host=db1.lab.local dbname=shop user=shop sslmode=verify-full sslrootcert=/etc/pki/tls/certs/lab-ca.crt"4Look around
psql -h db1 -U shop -d shop -c '\dt' psql -h db1 -U shop -d shop -c '\du'5Who is connected, what runs long?
psql -U postgres -c "SELECT pid, usename, state, now()-query_start AS age, left(query,60) FROM pg_stat_activity WHERE state <> 'idle' ORDER BY age DESC;"6Backup and restore one database
pg_dump -h db1 -U shop -Fc -d shop -f shop-$(date +%F).dump pg_restore -h db2 -U postgres -d shop --clean --if-exists shop-2026-10-27.dump7Back up roles and all databases
pg_dumpall -h db1 -U postgres --roles-only > roles.sql8Allow only the app subnet with passwords (scram)
9Size of databases and tables
psql -U postgres -c "SELECT datname, pg_size_pretty(pg_database_size(datname)) FROM pg_database ORDER BY pg_database_size(datname) DESC;"10Run in a container for a lab
podman run -d --name pg -e POSTGRES_PASSWORD=labonly -p 127.0.0.1:5432:5432 docker.io/library/postgres:16
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Never give the app the
postgressuperuser; one role per app, only the rights it needs. pg_hba.confis read top to bottom; a broadtrustline above opens everything.- Test your restores — a backup you never restored is a hope, not a backup.
الملفات
/var/lib/pgsql/data/postgresql.conf- Server settings (RHEL).
/var/lib/pgsql/data/pg_hba.conf- Who may connect how.
~/.pgpass- Saved passwords (chmod 600).
نصائح للحفظ
- On Kubernetes use an operator (CloudNativePG) for backups, failover and TLS.
رموز الخروج
0 | Success. |
1 | Fatal error (out of memory, file not found). |
2 | Connection lost. |
3 | Error in a script with ON_ERROR_STOP. |