如何设置PostgreSQL 9.6并访问Ubuntu 16.04上的PHPPGADMIN
PostgreSQL是由PostgreSQL全局开发组开发的强大而开源的对象关系数据库管理系统。
它给出了主要关注可扩展性和标准遵守情况。
这可以安全地存储数据,并允许用户在其他软件应用程序的请求下检索数据。
所有主要操作系统都支持,包括Linux,UNIX(AIX,BSD,HP-UX,SGI IRIX,Mac OS X,Solaris,Tru64)和Windows。
在本文中,我将解释如何在Ubuntu 16.04服务器上安装PostgreSQL 9.6.
如何安装postgreSQL
默认的Ubuntu存储库可能不包含我们所需的PostgreSQL版本,因此,我们需要创建自定义repo文件来下载/安装所需的版本。
我们可以使用以下内容创建一个APT源文件"/etc/apt/sources.list.d/postgresql.list"。
root@ubuntu:~# cat /etc/apt/sources.list.d/postgresql.list deb http://apt.postgresql.org/pub/repos/apt/xenial-pgdg main
现在我们可以通过执行以下命令来安装我们想要的postgresql版本:
root@ubuntu:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add OK root@ubuntu:~# apt-get update Hit:1 http://mirrors.linode.com/ubuntu xenial InRelease Hit:2 http://mirrors.linode.com/ubuntu xenial-updates InRelease Hit:3 http://mirrors.linode.com/ubuntu xenial-backports InRelease Hit:4 http://security.ubuntu.com/ubuntu xenial-security InRelease Get:5 http://apt.postgresql.org/pub/repos/apt xenial-pgdg InRelease [36.6 kB] Get:6 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 Packages [55.6 kB] Get:7 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main i386 Packages [55.5 kB] Fetched 148 kB in 0s (256 kB/s) Reading package lists... Done root@ubuntu:~# apt-get install postgresql-9.6 postgresql-contrib
我们可以通过记录到Postgres用户来确认PostgreSQL安装。
root@ubuntu:~# su - postgres postgres@ubuntu:~$psql psql (9.6.0) Type "help" for help.
管理/修改PostgreSQL中的用户角色
postgreSQL使用"角色"的概念管理数据库访问权限。
它可以是数据库用户,或者一组数据库用户,具体取决于角色的设置方式。
它们是一种类似于Linux用户帐户的方式。
任何角色都可以充当用户,组或者两者。
默认情况下,将在安装上设置postgres角色,该应用程序与带有shell访问的Linux用户名类似。
此角色具有管理权限,可以管理所有事务。
让我们看看我们如何从PSQL命令行创建新角色。
我使用此命令创建了一个名为testadmin的新角色。
我们可以从PG_Roles系统目录中确定现有角色集。
root@ubuntu:~# sudo -u postgres psql psql (9.6.0) Type "help" for help. postgres=# create role testadmin; CREATE ROLE postgres=# SELECT rolname FROM pg_roles; rolname ------------------ pg_signal_backend postgres testadmin (4 rows)
同样,我们可以使用命令"drop角色名称"删除用户角色。
我们还可以为此目的使用命令"CreateUser用户名或者DropUser用户名"。
查看以下命令:
root@ubuntu:~# su - postgres postgres@ubuntu:~$createuser --interactive Enter name of role to add: saheadmin Shall the new role be a superuser? (y/n) y
通过使用 - Interactive选项,我们可以使此过程交互。
我们可以使用帮助选项查看此命令的多个选项。
postgres@ubuntu:~$createuser --help createuser creates a new PostgreSQL role. Usage: createuser [OPTION]... [ROLENAME] Options: -c, --connection-limit=N connection limit for role (default: no limit) -d, --createdb role can create new databases -D, --no-createdb role cannot create databases (default) -e, --echo show the commands being sent to the server -E, --encrypted encrypt stored password -g, --role=ROLE new role will be a member of this role -i, --inherit role inherits privileges of roles it is a member of (default) -I, --no-inherit role does not inherit privileges -l, --login role can login (default) -L, --no-login role cannot login -N, --unencrypted do not encrypt stored password -P, --pwprompt assign a password to new role -r, --createrole role can create new roles -R, --no-createrole role cannot create roles (default) -s, --superuser role will be superuser -S, --no-superuser role will not be superuser (default) -V, --version output version information, then exit --interactive prompt for missing role name and attributes rather than using defaults --replication role can initiate replication --no-replication role cannot initiate replication -?, --help show this help, then exit Connection options: -h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port -U, --username=USERNAME user name to connect as (not the one to create) -w, --no-password never prompt for password -W, --password force password prompt Report bugs to <[email protected]>. postgres@ubuntu:~$
我们可以使用PSQL提示符使用此命令重置角色的密码。
root@ubuntu:~# sudo -u postgres psql psql (9.6.0) Type "help" for help. postgres=# ALTER USER testadmin PASSWORD 'password'; ALTER ROLE
如何设置角色属性
数据库角色可以具有许多属性,用于定义其权限并与客户端身份验证系统进行交互。
让我解释可以在角色创建期间使用的一些重要和有用的属性。
- 登录权限
- 超级用户
- 数据库创建
- 密码
- 角色创作
我们可以看到如何在创建期间将此属性分配给角色。
登录权限:与登录属性的角色可以将其视为与"数据库用户"相同的操作。我们可以使用以下命令传递此属性。
CREATE ROLE name LOGIN;
超级用户:我们可以使用此命令与超级权限作用。
CREATE ROLE name SUPERUSER;
数据库创建:可以使用以下命令创建数据库(超级用户除外)创建角色来创建角色,因为这些都绕过所有权限检查。
CREATE ROLE name CREATEDB;
密码:我们可以使用此命令在创建角色创建时指定密码。
CREATE ROLE name PASSWORD 'password string';
数据库管理
我们可以使用登录提示符或者使用命令从PSQL提示符创建命令创建数据库名称,在PostgreSQL中创建一个数据库:
来自登录shell:
postgres@ubuntu:~$createdb sahedb
来自psql提示符:
postgres=# CREATE DATABASE testdb; CREATE DATABASE
有时,我们还可以使用此命令在一个命令中为特定用户创建数据库。
角色将成为新数据库的所有者,因此他可以配置和管理它。
来自SQL提示符:
CREATE DATABASE dbname OWNER rolename;
来自shell:
createdb -O rolename dbname
同样,我们可以通过以下执行以下命令来销毁数据库:
来自SQL提示符:
DROP DATABASE name;
来自登录shell:
dropdb dbname
管理表
现在让我们看看我们如何为特定数据库创建表。
我们可以在数据库"testdb"中创建一个名为emporyees的表,我们在之前创建并将某些值插入表格。
连接到我们所需的DB:
root@ubuntu:~# su - postgres postgres@ubuntu:~$psql testdb psql (9.6.0) Type "help" for help.
创建表名员工,其中包含员工ID的三个文件,名字和姓氏。
testdb=# CREATE TABLE employees (employee_id int, first_name varchar, last_name varchar); CREATE TABLE
将以下详细信息插入表格并列出表内容。
testdb=# INSERT INTO employees VALUES (1, 'Joe', 'Sam'); INSERT 0 1 testdb=# SELECT * FROM employees; employee_id | first_name | last_name -------------+------------+---------- 1 | Joe | Sam
同样,我们可以使用以下命令删除表:
DROP TABLE tablename;
大多数SQL命令都与MySQL类似。
我们可以在此处获取有关表创建的更多选项。
如何安装phppgadmin
PHPPGADMIN提供了一种用于以非常简单的方式访问和管理PostgreSQL数据库的Web界面。
我们可以轻松创建数据库,数据库中的表,用户,存储过程等。
它还提供备份和还原的选项。
phppgadmin类似于phpmyadmin。
只需运行该命令即可安装此界面:apt-get安装phppgadmin
访问phppgadmin.
安装此工具后,我们需要编辑PHPPGADMIN配置文件以允许远程访问。
默认情况下,它本地允许访问。
让我们看看我们如何在远程公共IPS上获得访问权限。
我们可以编辑phppgadmin配置文件:/etc/phppgadmin/config.inc.php。
编辑以下行
$conf['extra_login_security'] = true;
将此值修改为"false"。
在这些更改后,请务必重新启动PostgreSQL和Apache 2服务。
root@ubuntu:/etc/phppgadmin# service postgresql restart root@ubuntu:/etc/phppgadmin# systemctl restart apache2
现在,我们可以使用URL >> http://ip/phppgadmin来访问phppgadmin /
我们可以从这里查看我们从命令行创建的所有数据库,甚至使用更多用户友好的选项从Web界面有效地管理它们。