PosgreSQL Basic

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: <h2>1. Install postgresql postgresql-contrib</h2> <span style="white-space:pre"></span> <p>camoss@cd-devel15:~$ sudo apt-get install postgresql postgresql-contrib</p> <p><br></p> This will ins

1. Install postgresql postgresql-contrib

camoss@cd-devel15:~$ sudo apt-get install postgresql postgresql-contrib


This will install the latest version available in your Ubuntu release and the commonly used add-ons for it.

2. Connect to PosgreSQL server

camoss@cd-devel15:~$ sudo -u <user> psql <database>

Note: 大多数PostgreSQL都会利用现有的Linux系统账户来认证PostgreSQL,所以,在安装PostgreSQL时,系统上必须有一个postgres的账户。
postgres账户作为PostgreSQL的管理员账户,而不是root。
基于PostgreSQL这种账户管理,我们必须以Linux账户postgres(或自己为Linux和PostgreSQL创建好的账户)来访问PostgreSQL服务器。
因此,你将看到PostgreSQL的管理员账户postgres在文件/etc/passwd中。
camoss@cd-devel15:~$ 
camoss@cd-devel15:~$ grep -i "postgres" /etc/passwd
postgres:x:113:122:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
camoss@cd-devel15:~$ 

e.g.
camoss@cd-devel15:~$ sudo -u postgres psql postgres

3. Set password for role/user of PostgreSQL

postgres=# \password <user>

e.g. 
Set a password for the "postgres" database role
postgres=# \password postgres

pwd: postgres_123

4. Show help information 

Show help information about the backslash commands

postgres=# \?

Show help information about SQL commands

postgres=# \h

5. Exit PosgreSQL server

Control+D
or
postgres=# \q

6. Create database

To create the first database, which we will call "demodb", simply type:
sudo -u postgres createdb demodb
or
camoss@cd-devel15:~$ sudo -u postgres psql postgres
postgres=# CREATE DATABASE testdb;

7. List all Databases

List the names, owners, character set encodings, and access privileges of all the databases in the server. 
If + is appended to the command name, database sizes, default tablespaces, and descriptions are also displayed. 
(Size information is only available for databases that the current user can connect to.)

postgres=# \l+ (or \list+)

8. Connect to Database

postgres=# \c or \connect [ dbname [ username ] [ host ] [ port ] ]

9. Create linux user for PosgreSQL

camoss@cd-devel15:~$ adduser demouser

pwd: demopwd

10. Create/Drop database role/user

camoss@cd-devel15:~$ sudo -u postgres createuser demouser

camoss@cd-devel15:~$ sudo -u postgres dropuser demouser

or
postgres=# CREATE ROLE demouser;
postgres=# ALTER ROLE demouser login;
or

postgres=# CREATE ROLE demouser login;


Note: 我试了一下sudo -u postgres createuser <user/role> 和 CREATE ROLE <role/user> login 创建的user/role效果一样。

可以通过\du或\dg来查看database roles/users

e.g.

Creates the user with no database creation rights (-D) with no add user rights (-A) and will prompt you for entering a password (-P)

camoss@cd-devel15:~$ sudo -u postgres createuser -D -A -P myuser

Create the database 'mydb' with 'myuser' as owner

camoss@cd-devel15:~$ sudo -u postgres createdb -O myuser mydb

11. Alter database

postgres=# ALTER DATABASE name RENAME TO new_name
postgres=# ALTER DATABASE name OWNER TO new_owner
postgres=# ALTER DATABASE name SET TABLESPACE new_tablespace

e.g.
demodb=# 
demodb=# ALTER DATABASE demodb OWNER TO demouser;
ALTER DATABASE
demodb=# 
demodb=# \l

12. Connect to database(demodb) of PosgreSQL serveras above user(demouser)

Note:
database "demodb" --> which is created by above step 7
user "demouser" --> which is created by above steps 10 and 11

e.g.
camoss@cd-devel15:~$ 
camoss@cd-devel15:~$ sudo -u demouser psql demodb
psql (9.3.6)
Type "help" for help.
demodb=> 
demodb=> 

13. List Users and Roles

\du[+] [ pattern ]
Lists database roles. (Since the concepts of "users" and "groups" have been unified into "roles", this command is now equivalent to \dg.) 
If pattern is specified, only those roles whose names match the pattern are listed. 
If the form \du+ is used, additional information is shown about each role; currently this adds the comment for each role.

\dg[+] [ pattern ]
Lists database roles. (Since the concepts of "users" and "groups" have been unified into "roles", this command is now equivalent to \du.) 
If pattern is specified, only those roles whose names match the pattern are listed. 

If the form \dg+ is used, additional information is shown about each role; currently this adds the comment for each role.

14. Enable PostgreSQL to listen across different networks

Edit file /etc/postgresql/9.3/main/postgresql.conf, and alter the listen_addresses 
e.g. Listen on all network interfaces as follow:
listen_addresses = '*'

15. Reload/Restart PostgreSQL Server

Configuring the networking / users may need to reload the server

camoss@cd-devel15:~$ sudo /etc/init.d/postgresql reload

Some settings changes in postgresql.conf require a full restart

camoss@cd-devel15:~$ sudo /etc/init.d/postgresql restart


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
安全 jenkins 持续交付
Jenkins常用插件介绍之权限控制插件Role-based Authorization Strategy
除了搭建jenkins时默认安装的插件之外,有时候扩展功能,还需要安装一些其他的插件,下面为大家简单介绍一下Role-based Authorization Strategy插件。
Jenkins常用插件介绍之权限控制插件Role-based Authorization Strategy
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
SAP WM初阶Interim Storage Type不好启用Storage Unit Management
|
测试技术 数据安全/隐私保护 安全
|
Shell Windows

热门文章

最新文章