PostgreSQL:修订间差异
跳到导航
跳到搜索
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
(→常用指令) |
(→安裝) |
||
(未显示同一用户的25个中间版本) | |||
第1行: | 第1行: | ||
'''PostgreSQL'''是一套[[RDBMS]]。 | '''PostgreSQL'''是一套[[RDBMS]]。 | ||
== 安裝 == | |||
這是[[Ubuntu]]下的安裝: | |||
<syntaxhighlight lang="bash"> | |||
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc; sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'; sudo apt update; sudo apt install -y postgresql; sudo apt clean | |||
</syntaxhighlight> | |||
== 設定 == | |||
=== postgresql.conf === | |||
PostgreSQL 14的設定檔在<code>/etc/postgresql/14/main</code>下,其他版本的依此類推,通常會修改<code>postgresql.conf</code>將本來只聽localhost的改成聽所有界面,可以配合後面會提到的<code>pg_hba.conf</code>,但要注意安全性(像是用防火牆只開放特定連線): | |||
<syntaxhighlight lang="ini"> | |||
listen_addresses = '*' | |||
</syntaxhighlight> | |||
另外可以修改<code>work_mem</code>,預設是4MB,調大一點會對於排序相關的操作有幫助<ref>{{Cite web |url=https://aws.amazon.com/blogs/database/tune-sorting-operations-in-postgresql-with-work_mem/ |title=Tune sorting operations in PostgreSQL with work_mem |language=en |accessdate=2023-04-25 |date=2021-10-16}}</ref>,如果整台機器都是給PostgreSQL用的,可能的公式是把整台機器記憶體大小乘以0.8,扣掉<code>shared_buffers</code>再除以最大連線數量: | |||
<syntaxhighlight lang="ini"> | |||
work_mem = 16MB | |||
</syntaxhighlight> | |||
如果是共用的機器,可以保守一點。 | |||
=== pg_hba.conf === | |||
另外修改<code>pg_hba.conf</code>讓外面可以連入,這邊開放<codE>10.0.0.0/8</code>可以連: | |||
<syntaxhighlight lang="bash"> | |||
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 | |||
</syntaxhighlight> | |||
== 常用指令 == | == 常用指令 == | ||
建立資料庫 | 剛裝好的系統可以透過<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> : | |||
<syntaxhighlight lang="sql"> | <syntaxhighlight lang="sql"> | ||
CREATE USER exampleuser WITH ENCRYPTED PASSWORD 'password'; | CREATE USER exampleuser WITH ENCRYPTED PASSWORD 'password'; | ||
GRANT postgres TO exampleuser; | |||
CREATE DATABASE exampledb OWNER = exampleuser; | |||
</syntaxhighlight> | |||
授權給其他使用者操作: | |||
<syntaxhighlight lang="sql"> | |||
GRANT ALL PRIVILEGES ON DATABASE exampledb TO exampleuser; | GRANT ALL PRIVILEGES ON DATABASE exampledb TO exampleuser; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
超級使用者: | |||
<syntaxhighlight lang="sql"> | |||
ALTER USER exampleuser WITH SUPERUSER; | |||
</syntaxhighlight> | |||
== 管理 == | |||
列出所有<code>database_name</code>下的schema,類似[[MySQL]]裡的<code>SHOW CREATE TABLE</code>: | |||
<syntaxhighlight lang="bash"> | |||
pg_dump -d database_name -s -h hostname -U username | |||
</syntaxhighlight> | |||
== 參考資料 == | |||
{{Reflist|2}} | |||
== 外部連結 == | == 外部連結 == |
2024年1月21日 (日) 01:00的最新版本
PostgreSQL是一套RDBMS。
安装
这是Ubuntu下的安装:
sudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc; sudo sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'; sudo apt update; sudo apt install -y postgresql; sudo apt clean
设定
postgresql.conf
PostgreSQL 14的设定档在/etc/postgresql/14/main
下,其他版本的依此类推,通常会修改postgresql.conf
将本来只听localhost的改成听所有界面,可以配合后面会提到的pg_hba.conf
,但要注意安全性(像是用防火墙只开放特定连线):
listen_addresses = '*'
另外可以修改work_mem
,预设是4MB,调大一点会对于排序相关的操作有帮助[1],如果整台机器都是给PostgreSQL用的,可能的公式是把整台机器内存大小乘以0.8,扣掉shared_buffers
再除以最大连线数量:
work_mem = 16MB
如果是共用的机器,可以保守一点。
pg_hba.conf
另外修改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
建立账号与数据库,这边建立数据库的部分用两段式建立是为了闪开操作时的权限问题[2]:
CREATE USER exampleuser WITH ENCRYPTED PASSWORD 'password';
GRANT postgres TO exampleuser;
CREATE DATABASE exampledb OWNER = 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
参考资料
- ↑ Tune sorting operations in PostgreSQL with work_mem. 2021-10-16 [2023-04-25] (English).
- ↑ "ERROR: must be member of role" When creating schema in PostgreSQL. [2021-11-12] (English).
外部链接
- 官方网站 (英文)