archlinux 使用 postgresql

一、准备操作

默认安装后已创建 postgres 系统用户

切换用户

$ sudo -iu postgres # Or su - postgres for root

数据目录初始化

[postgres]$ initdb --locale=zh_CN.UTF-8 -E UTF8 -D /var/lib/postgres/data

退出此用户,启动服务 postgresql.service

二、常见操作

再次进入 sudo -iu postgres,

添加新的数据库用户

$ createuser --interactive

创建数据库

$ createdb myDatabaseName

  

登录 psql shell

psql -d myDatabaseName
#查看所有数据库
\l
#切换数据库
\c <database>
#列出所有用户及权限
\du
#列出当前数据库所有表
\dt
三、配置文件

设置远程访问

/var/lib/postgres/data/postgresql.conf
listen_addresses = 'localhost,my_local_ip_address'
四、排错及其它

查看日志

journalctl -u postgresql.service

修改模板 UTF-8

First, we need to drop template1. Templates cannot be dropped, so we first modify it so it is an ordinary database:

UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;

The next step is to create a new database from template0, with a new default encoding.modify template1 so it is actually a template:

UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';

Now you can create a new database:

[postgres]$ createdb blog


233

猜你喜欢

转载自www.cnblogs.com/lemos/p/11605557.html