psql 常用命令

1. 查看数据库

	psql -l
	\l

2. 连接数据库

	psql -h ip -p port dbname user name
	
	psql -U user -d dbname

        psql dbname

        alter table parent_mch OWNER to testdev

        sudo -u postgres psql -d db223 // log in as super admin


3. 查看数据表

	所有数据表
	\d

	单个表
	\d tablename

	更多信息
	\d+ tablename 

	\dt
	\di 索引
	\ds 序列
	\dv 视图
	\df 函数
	
4. 显示sql 执行的时间

	\timing on 
	sql 

5. 列出所有的角色和用户

	\du
	\dg
	
6. 把每行中每列数据拆成单行数据显示

	\x

7. 执行外部存储sql

    \i

8. postgres 默认自动提交

	\set AUTOCOMMIT off

9. psql 更改密码

a

PostgreSQL默认的超级管理员密码是postgres
连接方法:
su - postgres 或 sudo su - postgres
psql -U postgres(注意,是大写的-U)默认密码为空
修改密码的方法是,用psql登入管理:psql -U postgres
然后用这样的命令来修改密码:alter user postgres with password 'new password'
b

首先打开data目录下的pg_hba.conf配置文件,找到:

# IPv4 local connections: host all all 127.0.0.1/32 md5

然后把md5改成trust,重新启动。

psql -d template1 -U postgres -c "alter role postgres password ‘123456’;"

注:此时trust是否改回来都无足轻重了。
c用客户端pgadmin修改


猜你喜欢

转载自37235798.iteye.com/blog/2276837