{"id":216,"date":"2026-03-08T14:35:36","date_gmt":"2026-03-08T14:35:36","guid":{"rendered":"https:\/\/blogs.giamkichsan.com\/?p=216"},"modified":"2026-03-08T15:08:28","modified_gmt":"2026-03-08T15:08:28","slug":"postgresql-tren-ubuntu","status":"publish","type":"post","link":"https:\/\/blogs.giamkichsan.com\/index.php\/2026\/03\/08\/postgresql-tren-ubuntu\/","title":{"rendered":"PostgreSQL tr\u00ean Ubuntu"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">1. C\u00e0i PostgreSQL<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt upgrade -y\nsudo apt install postgresql postgresql-contrib -y\n   -- postgresql: database server\n   -- postgresql-contrib: c\u00e1c extension h\u1eefu \u00edch\nsudo systemctl status postgresql -- Ki\u1ec3m tra tr\u1ea1ng th\u00e1i PostgreSQL<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u0110\u0103ng nh\u1eadp v\u00e0o PostgreSQL<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<h5 class=\"wp-block-heading\">2.1 PostgreSQL t\u1ea1o user m\u1eb7c \u0111\u1ecbnh l\u00e0 <span style=\"background-color: initial; font-family: inherit; font-size: inherit; text-align: initial; color: initial;\">postgres<\/span>.<\/h5>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo -i -u postgres\npsql\n-- N\u1ebfu th\u00e0nh c\u00f4ng b\u1ea1n s\u1ebd th\u1ea5y:\npostgres=#\n-- Tho\u00e1t:\n\\q<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.2 T\u1ea1o database v\u00e0 user m\u1edbi (khuy\u1ebfn ngh\u1ecb)<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">-- T\u1ea1o user:\nCREATE USER username WITH PASSWORD 'password';\n\n-- T\u1ea1o database:\nCREATE DATABASE dbname;\n\n-- T\u1ea1o v\u00e0 G\u00e1n database cho user:\nCREATE DATABASE mydb OWNER myuser;\n\n-- \u0110\u1ed5i m\u1eadt kh\u1ea9u:\nALTER USER username WITH PASSWORD 'new_password';<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">2.3 Ki\u1ec3m tra version <\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>psql --version<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u0110\u0103ng nh\u1eadp t\u1eeb Terminal<\/h4>\n\n\n\n<p>C\u00fa ph\u00e1p:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql -U username -d database_name -h localhost -W<\/pre>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">psql -U myuser -d mydb -h localhost -W<\/pre>\n\n\n\n<p>Gi\u1ea3i th\u00edch:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-U<\/code> : username PostgreSQL<\/li>\n\n\n\n<li><code>-d<\/code> : database mu\u1ed1n truy c\u1eadp<\/li>\n\n\n\n<li><code>-h<\/code> : host (localhost)<\/li>\n\n\n\n<li><code>-W<\/code> : y\u00eau c\u1ea7u nh\u1eadp password<\/li>\n<\/ul>\n\n\n\n<p>Sau \u0111\u00f3 nh\u1eadp <strong>password<\/strong> c\u1ee7a user.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. C\u1ea5p quy\u1ec1n (n\u1ebfu c\u1ea7n)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Cho ph\u00e9p user t\u1ea1o database:\nALTER USER devuser CREATEDB;\n\n-- Cho ph\u00e9p quy\u1ec1n admin <em>(ch\u1ec9 d\u00f9ng khi th\u1eadt s\u1ef1 c\u1ea7n)<\/em>:\nALTER USER devuser WITH SUPERUSER;\n\n-- Ki\u1ec3m tra danh s\u00e1ch user:\n\\du\n\n-- \u0110\u0103ng nh\u1eadp b\u1eb1ng user m\u1edbi\npsql -U devuser -d devdb -h localhost -W<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">6. Ki\u1ec3m tra Post b\u1eb1ng c\u00e1ch Ki\u1ec3m tra trong file c\u1ea5u h\u00ecnh (chu\u1ea9n nh\u1ea5t)<\/h5>\n\n\n\n<p>M\u1edf file: sudo nano \/etc\/postgresql\/*\/main\/postgresql.conf<\/p>\n\n\n\n<p>T\u00ecm d\u00f2ng: port = 5432<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">7. N\u1ebfu b\u1ea1n mu\u1ed1n login PostgreSQL b\u1eb1ng password t\u1eeb localhost, c\u00f3 th\u1ec3 c\u1ea7n ch\u1ec9nh file<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">\/etc\/postgresql\/&lt;version&gt;\/main\/pg_hba.conf<\/pre>\n\n\n\n<p>\u0111\u1ed5i: peer => th\u00e0nh: md5<\/p>\n\n\n\n<p>Sau \u0111\u00f3 restart: sudo systemctl restart postgresql<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- N\u1ebfu kh\u00f4ng login \u0111\u01b0\u1ee3c: \u0111\u1ed5i md5 sang trust\n-- Restart: sudo systemctl restart postgresql\n-- \u0111\u1ed5i m\u1eadt kh\u1ea9u: ALTER USER postgres WITH PASSWORD 'newpassword';\n-- \u0111\u1ed5i l\u1ea1i t\u1eeb trust v\u1ec1 md5 \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o b\u1ea3o m\u1eadt\n-- Restart: sudo systemctl restart postgresql<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>1. C\u00e0i PostgreSQL 2. \u0110\u0103ng nh\u1eadp v\u00e0o PostgreSQL 2.1 PostgreSQL t\u1ea1o user m\u1eb7c \u0111\u1ecbnh l\u00e0 postgres. 2.2 T\u1ea1o database v\u00e0 user m\u1edbi (khuy\u1ebfn ngh\u1ecb) &#8212; <a class=\"mh-excerpt-more\" href=\"https:\/\/blogs.giamkichsan.com\/index.php\/2026\/03\/08\/postgresql-tren-ubuntu\/\" title=\"PostgreSQL tr\u00ean Ubuntu\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-216","post","type-post","status-publish","format-standard","hentry","category-https-blogs-giamkichsan-com-index-php-category-ubuntu-he-dieu-hanh"],"_links":{"self":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":17,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/216\/revisions\/234"}],"wp:attachment":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/categories?post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/tags?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}