「PostgreSQL」:修訂間差異

出自Gea-Suan Lin's Wiki
跳至導覽 跳至搜尋
本頁面具有訪問限制。如果您看見此訊息,這代表您沒有訪問本頁面的權限。
無編輯摘要
行 8: 行 8:


== 設定 ==
== 設定 ==
剛裝好的系統可以透過<code>postgres</code>這個帳號權限連進去:
<syntaxhighlight lang="bash">
sudo -u postgres psql
</syntaxhighlight>


PostgreSQL 14的設定檔在<code>/etc/postgresql/14/main</code>下,通常會修改<code>postgresql.conf</code>將本來只聽localhost的改成聽所有界面:
PostgreSQL 14的設定檔在<code>/etc/postgresql/14/main</code>下,通常會修改<code>postgresql.conf</code>將本來只聽localhost的改成聽所有界面:
行 28: 行 22:


== 常用指令 ==
== 常用指令 ==
剛裝好的系統可以透過<code>postgres</code>這個帳號權限連進去:
<syntaxhighlight lang="bash">
sudo -u postgres psql
</syntaxhighlight>


 建立帳號與資料庫,這邊建立資料庫的部份用兩段式建立是為了閃開操作時的權限問題<ref>{{Cite web |url=https://stackoverflow.com/questions/26684643/error-must-be-member-of-role-when-creating-schema-in-postgresql |title="ERROR: must be member of role" When creating schema in PostgreSQL |language=en |accessdate=2021-11-12}}</ref>:
 建立帳號與資料庫,這邊建立資料庫的部份用兩段式建立是為了閃開操作時的權限問題<ref>{{Cite web |url=https://stackoverflow.com/questions/26684643/error-must-be-member-of-role-when-creating-schema-in-postgresql |title="ERROR: must be member of role" When creating schema in PostgreSQL |language=en |accessdate=2021-11-12}}</ref>:

於 2023年4月25日 (二) 07:33 的修訂

PostgreSQL是一套RDBMS

安裝

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'; wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -; sudo apt update; sudo apt install -y postgresql; sudo apt clean

設定

PostgreSQL 14的設定檔在/etc/postgresql/14/main下,通常會修改postgresql.conf將本來只聽localhost的改成聽所有界面:

listen_addresses = '*'

另外修改pg_hba.conf讓外面可以連入,這邊開放10.0.0.0/8可以連:

echo "host all all 10.0.0.0/8 scram-sha-256" | sudo tee -a /etc/postgresql/14/main/pg_hba.conf; sudo service postgresql restart

常用指令

剛裝好的系統可以透過postgres這個帳號權限連進去:

sudo -u postgres psql

建立帳號與資料庫,這邊建立資料庫的部份用兩段式建立是為了閃開操作時的權限問題[1]

CREATE USER exampleuser WITH ENCRYPTED PASSWORD 'password';
CREATE DATABASE exampledb;
ALTER DATABASE exampledb OWNER TO exampleuser;

授權給其他使用者操作:

GRANT ALL PRIVILEGES ON DATABASE exampledb TO exampleuser;

超級使用者:

ALTER USER exampleuser WITH SUPERUSER;

管理

列出所有database_name下的schema,類似MySQL裡的SHOW CREATE TABLE

pg_dump -d database_name -s -h hostname -U username

參考資料

外部連結