PostgreSQL:修订间差异

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
无编辑摘要
第9行: 第9行:
== 設定 ==
== 設定 ==


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


<syntaxhighlight lang="bash">
PostgreSQL 14的設定檔在<code>/etc/postgresql/14/main</code>下,其他版本的依此類推,通常會修改<code>postgresql.conf</code>將本來只聽localhost的改成聽所有界面:
 
<syntaxhighlight lang="ini">
listen_addresses = '*'
listen_addresses = '*'
</syntaxhighlight>
</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>:
<syntaxhighlight lang="ini">
work_mem = 16MB
</syntaxhighlight>
=== pg_hba.conf ===


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

2023年4月25日 (二) 07:36的版本

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.conf

PostgreSQL 14的设定档在/etc/postgresql/14/main下,其他版本的依此类推,通常会修改postgresql.conf将本来只听localhost的改成听所有界面:

listen_addresses = '*'

另外可以修改work_mem,预设是4MB,调大一点会对于排序相关的操作有帮助[1]

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';
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

参考资料

外部连结