---
# System prepended metadata

title: 基本操作
tags: [PostgreSQL]

---

###### tags: `PostgreSQL`

# 基本操作

## 命令列建立 Role
```bash
$ sudo -i -u postgres
$ createuser --interactive --pwprompt

```

## 建立資料庫

```sql
CREATE DATABASE db_name ENCODING 'UTF8'  LC_COLLATE  'zh_TW.UTF-8' LC_CTYPE 'zh_TW.UTF-8';
```

## 列出資料庫
```sql
SELECT datname FROM pg_database;
```

## 設定權限
```sql
GRANT ALL privileges ON DATABASE kingone TO kingone;
\c kingone
grant all on all tables in schema public to kingone;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO kingone;
```
 
## 匯入 sql
```bash
$ sudo -i -u postgres
$ psql databasename < dump_file.sql
```

