在 ubuntu 安裝:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
linux shell 模式下常用指令:
sudo -i -u postgres // 切換 postgres 的使用者身分,用來操作資料庫
createuser -P peter // 使用 createuser 這隻指令來設定一個使用者 peter ,並且提示設定密碼
createdb peterdb // 使用 createdb 這隻指令來建立一個資料庫 peterdb
dropuser peter
pg_dump --column-inserts --data-only -d [database_name] -t [table_name] > file.sql // 備分資料庫
連進資料庫:
psql
又或是在連進資料庫之後,建立使用者 enzo/資料庫 plenish / 給予權限:
CREATE DATABASE enzo;
CREATE ROLE plenish WITH LOGIN PASSWORD 'plenish';
GRANT ALL ON DATABASE enzo TO plenish;
連線後常用指令:
\l // 列出所有的資料庫
\d // 列出所有的資料表
\d+ [tablename] // 顯示 table 的 schema
\c [databasename] // 切換其它的資料庫
ALTER SEQUENCE users_id_seq RESTART WITH 1000; // 將 user_id_seq 這個 sequence 設定成從 1000 開始起跳。
< 範例 1 > (可用於 luminus 專案開發)
create database mydb;
create user myuser with encrypted password 'mypass';
grant all privileges on database mydb to myuser;
// 建立特定的使用者、資料庫、並且給予完整的權限
< 範例 2 >
alter user <username> with encrypted password '<password>';
< 範例 3 >
alter user <username> createdb;