關於postgresql 常用操作指令

2021-12-30 02:32:49 字數 1453 閱讀 7803

關於postgresql——常用操作指令

建立資料庫

create database test with owner = postgres encoding = 'utf8';

進入控制台方法,在postgresql的安裝目的bin下執行命令: psql 資料庫名,

例: /usr/local/pgsql/bin/psql mydb

指定host、使用者名稱和資料庫的話,如:  www.2cto.com  

/usr/local/pgsql/bin/psql -h localhost -u postgres -d test

檢視版本: psql --version 或 select version();

檢視所有資料庫:\l

檢視所有資料庫(包括詳細引數):select * from pg_database;

選擇資料庫:\c databasename

檢視所有表:\dt

檢視某個表的結構:\d tablename

退出psql控制台:\q

檢視表的索引:

select * from pg_indexes where tablename='log';

匯出備份資料庫:

pg_dump -h localhost -u postgres databasename > /tmp/databasename.bak.yyyymmdd.sql

匯入恢復資料庫(sql檔案是pg_dump匯出的檔案就行,可以是整個資料庫,也可以只是單個表,也可以只是結構等):

psql -h localhost -u postgres -d databasename < /tmp/databasename.bak.yyyymmdd.sql

匯出資料結構,主要是加上引數-s:

pg_dump -u username -w dbname -f /tmp/filename.sql

匯出某個表:

pg_dump -h localhost -u postgres -t tablename dbname > test.sql

匯出某個表的結構,同樣是加引數"-s":

pg_dump -h localhost -u postgres -t tablename -s dbname > test_construct.sql

匯出某個表的資料,加引數"-a":

pg_dump -h localhost -u postgres -t tablename -a dbname > test_data.sql

檢視序列:select * from information_schema.sequences where sequence_schema = 'public';

檢視資料庫大小:select pg_size_pretty(pg_database_size('test'));

檢視表的大小:select pg_size_pretty(pg_relation_size('test'));

關於PostgreSQL編碼

開始學習db sql,用的是postgresql,字元編碼問題鬧心了。查閱網上資料,焦點集中在以下三個方面 資料庫伺服器編碼 資料庫客戶端編碼 本地環境編碼。檢視postgresql server編碼的方法 postgres show server encoding server encoding ...

postgreSQL常用函式

select coalesce sum duration 0 若sum duration 返回值為空,則為其賦值0 select to date 2013 12 20 yyyy mm dd 字串轉化為date型別 select date 2013 10 28 01 00 interval 50 mi...

常用postgresql命令

1.初始化postgresql資料庫 方法1 usr pgsql 9.4 bin initdb d opt pgsql data 方法2 sudo service postgresql initdb 2.啟動postgresql服務 sudo service postgresql start sta...