Technical architectural analysis, practical configuration manifests, and deployment instructions for PostgreSQL Database Admin: MVCC & Indexing.
Basic
CREATE USER keycloak PASSWORD 'keycloak' SUPERUSER;
CREATE DATABASE test_keycloak OWNER keycloak;
-- **DB ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ**
CREATE DATABASE *dbname* OWNER *ownnerName*
DB Access
-- **DB ์ ์**
psql -U [DB์ฌ์ฉ์๊ณ์ ] [๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
]
-- **Postgre SQL shell ์ง์
์**
psql [์คํค๋ง๋ช
]
psql #default account postgres
psql -U {user} -d {database} -c "{query}" #postgres ์ ์ , airflow Database(schema)
DB System
Grant
GRANT ALL PRIVILEGES ON ALL TABLES IN schema public TO postgres;
GRANT ALL PRIVILEGES ON DATABASE keycloak TO postgres;
Show
show stuffs
-- Show CONFIG_PATH
show config_file;
Table
-- **DB ์คํ์ค์ธ ์ฟผ๋ฆฌ ์กฐํ**
SELECT * FROM pg_stat_activity ORDER BY query_start ASC;
-- Setting ์กฐํ
SELECT name, setting FROM pg_settings;
--
SELECT * FROM PG_DATABASE;
SELECT * FROM PG_USER;
select * from PG_ROLES;
-- ํ
์ด๋ธ ๋ฆด๋ ์ด์
์ฌ์ด์ฆ ์กฐํ
SELECT schemaname, tablename, pg_size_pretty(pg_total_relation_size( schemaname || '.' || tablename )) AS size
FROM pg_tables
WHERE NOT (schemaname = 'pg_catalog' OR schemaname = 'information_schema')
ORDER BY pg_total_relation_size( schemaname || '.' || tablename ) DESC;
Expression
-- **DB ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ถ๋ ฅ**
\l or \list
-- **DB ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ํ**
\c [๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ช
]
-- **DB ํ
์ด๋ธ ์ถ๋ ฅ**
\dt
-- **ํ
์ด๋ธ ๊ตฌ์กฐ ์กฐํ**: ์ค๋ผํด์ DESCRIBE TABLE # \d+ ํ
์ด๋ธ๋ช
# \d+ ํ
์ด๋ธ๋ช
-- **DB ํด๋น ํ
์ด๋ธ ์ ๋ณด ์ถ๋ ฅ**
\d [ํ
์ด๋ธ๋ช
]
\d+ [ํ
์ด๋ธ๋ช
]
-- **DB ๋๊ฐ๊ธฐ(์ข
๋ฃ)**
\q
-- **DB ์ฌ์ฉ์ ๊ถํ ์ ๋ณด**
\du
-- **DB ์ถ๋ ฅ ๋ณ๊ฒฝ**
\x
-- **DB ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ํ์ผ ์ ์ฅ(์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅํ์ง ์๊ณ ํ์ผ์ ์ ์ฅ๋จ)**
\o [ํ์ผ๊ฒฝ๋ก]
\l # list of databases(schema)
\r # clear query buffer
\q # quit session
\{}+ # additional information
\dt # List of relations(type=table)
\ds # List of relations(type=sequence)
\du # List of roles on Users
\d {table} # Show scheme for Table public.job
Backup & Restore
# **SQL๋ฌธ ํ์ผ์ ์คํ** $ psql -U [์ฌ์ฉ์๋ช
] [DB๋ช
] < [SQLํ์ผ๋ช
]
# **ํ์ผ๋ก๋ถํฐ DB๋ก dump, ๋๋ DB๋ก๋ถํฐ ํ์ผ๋ก dump** # COPY ํ
์ด๋ธ๋ช
[(์ปฌ๋ผ๋ช
1, ์ปฌ๋ผ๋ช
2,...)] FROM 'ํ์ผ๋ช
' # COPY ํ
์ด๋ธ๋ช
[(์ปฌ๋ผ๋ช
1, ์ปฌ๋ผ๋ช
2,...)] TO 'ํ์ผ๋ช
' ๊ตฌ๋ถ์๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ํญ์ผ๋ก ์ง์ ๋จ. ์ ์ฒด ์ต์
์ ์๋์ ๊ฐ์.[http://www.postgresql.org/docs/9.2/static/sql-copy.html](http://www.postgresql.org/docs/9.2/static/sql-copy.html)์ฐธ๊ณ
# **ํ์ผ๋ก dump**
pg_dump -d [db๋ช
] -U [user] -F t > [ํ์ผ๋ช
.tar]
- d, --dbname : Backupํ Database ๋ช
.
- h, --host : Database ์ฃผ์.
- U, --username : Database ์ ์ ์ User ID
- F, --format : Backup Format. ํ์๋ ์ฃผ๋ก tar ํ์ผ๋ก backupํ๊ธฐ ๋๋ฌธ์ 't'๋ฅผ ์ฌ์ฉํ๋ค.
- f, --file : Backup File Name
- t, --table : ํน์ Table๋ง Backupํ๋ คํ ๋ ๋์์ด ๋๋ Table ๋ช
- j, --jobs : Backup ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์ฌ๋ถ์ ๊ทธ ์ ๋.
- v, --verbose : ์งํ ๊ณผ์ ํ์.
- **dump to restore**
pg_restore -h localhost -U postgres -C -d postgres -F t [dumpFile]
- d, --dbname : Restoreํ๋ Database ๋ช
.
- h, --host : Database ์ฃผ์.
- U, --username : Database ์ ์ ์ User ID
- F, --format : Restore File์ Format.
- t, --table : ํน์ Table๋ง Restoreํ๋ คํ ๋ ๋์์ด ๋๋ Table ๋ช
- j, --jobs : Restore ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์ฌ๋ถ์ ๊ทธ ์ ๋.
- v, --verbose : ์งํ ๊ณผ์ ํ์
- C, --create : Target DB๋ฅผ ์๋ก ๋ง๋ค๋ฉด์ Restoration ์งํ.
- c, --clean : Restoration ์์ ๊ฐ์ ์ด๋ฆ์ Database Object๊ฐ ๋ฐ๊ฒฌ๋๋ฉด Drop ํ์ Createํ๊ฒ ํจ.
- O, --no-owner : ์๋ณธ DB์ Owner๊ฐ ๋ณต๊ตฌํ ์์น์ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ๋ณต๊ตฌ ์ ๋ค๋์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ์ด๋ฅผ ๋ง๊ธฐ ์ํด DB ๋ณต๊ตฌ์ OWNER๋ฅผ ๋ช
์ํ์ง ์๊ณ ์งํํ๊ฒ ํจ.
Spacing
โ Clear storage used by DB after Delete
In PostgreSQL, both โVACUUMโ and โVACUUM FULLโ are used to manage and optimize database storage, but their behavior and purpose are different. The differences between the two are as follows:
- VACUUM:
โVACUUMโ is a command used to secure storage space and improve performance by removing dead rows, updating statistics, etc. When you perform a data modification (such as
DELETE,UPDATE, orINSERT), PostgreSQL marks the previous version of the row as โdeadโ but does not immediately remove it. This can lead to fragmentation and inefficiency.
The โVACUUMโ command has several modes and can be run without options to perform routine cleanup, remove dead rows, and update statistics for all tables and indexes in the database. This is a non-blocking operation. This means it can be done while the database is online and accessible.
VACUUM;
You can also target individual tables or indexes using specific options with โVACUUMโ.
- โVACUUM FULLโ typically locks the table only for the duration of the operation and rewrites the entire table, which can be time-consuming and resource-intensive, so it is generally not recommended for general use. This is typically used when significant amounts of space need to be reclaimed after major data modifications.
2. VACUUM FULL:VACUUM FULLis a more aggressive form of theVACUUMcommand. Reclaim storage space by creating a new copy of the table, copying only the active rows, and then replacing the original table with the new copy. This process helps reduce fragmentation and optimize storage. However, because you are creating a new table, you will need an exclusive lock on the table being VACUUMed to effectively prevent any other operations on the table while the VACUUM FULL process is in progress.
VACUUM FULL your_table;
Due to exclusive locking and potential performance impact, โVACUUM FULLโ is generally used with caution and only in situations where significant storage reclamation is required.
In summary, consider using VACUUM on a regular basis for routine maintenance and space reclamation, and using VACUUM FULL sparingly, only when you need to perform a large cleanup with the intention of locking the table exclusively for a period of time. Always perform these operations in a controlled manner, considering the impact on the database and ongoing operations.
Features
Airflow
- delete failed tasks
# psql node
sudo -i
su -- postgres
psql -U postgres -d airflow -c "delete from dag_run where dag_id='${DAG_ID}' AND state='failed';"




Leave a Reply