Deepin V20 Linux 安装 postgresql完美方案

从官方源安装,配置官方源。

sudo apt-get install curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Create /etc/apt/sources.list.d/pgdg.list. The distributions are called codename-pgdg. In the example, replace buster with the actual distribution you are using:

deb http://apt.postgresql.org/pub/repos/apt buster-pgdg main

(You may determine the codename of your distribution by running lsb_release -c). For a shorthand version of the above, presuming you are using a supported release:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Finally, update the package lists, and start installing packages:

sudo apt-get update
sudo apt-get install postgresql-11 pgadmin4

由于Deepin 20 基于Debian buster 10.3,可能需要更新,没关系,查看一下哪些需要更新,再更新就好了。

 apt list --upgradable

添加新用户和新数据库新建一个Linux新用户,设置密码

adduser dbuser

切换到postgres用户,将在postgres用户下创建新数据库用户:

sudo su - postgres

登录数据库: 

psql

设置密码:

\password postgres

创建数据库用户dbuser(刚才创建的是Linux系统用户),并设置密码:

CREATE USER dbuser WITH PASSWORD 'password';

创建用户数据库,这里为exampledb,并指定所有者为dbuser:

CREATE DATABASE exampledb OWNER dbuser;

将exampledb数据库的所有权限都赋予dbuser:

GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser;

退出控制台

\q

登录数据库

psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432

控制台命令:

  • \h:查看SQL命令的解释,比如\h select。

  • \?:查看psql命令列表。

  • \l:列出所有数据库。

  • \c [database_name]:连接其他数据库。

  • \d:列出当前数据库的所有表格。

  • \d [table_name]:列出某一张表格的结构。

  • \du:列出所有用户。

  • \e:打开文本编辑器。

  • \conninfo:列出当前数据库和连接的信息。

数据库操作

# 创建新表 
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);

# 插入数据 
INSERT INTO user_tbl(name, signup_date) VALUES('张三', '2013-12-22');

# 选择记录 
SELECT * FROM user_tbl;

# 更新数据 
UPDATE user_tbl set name = '李四' WHERE name = '张三';

# 删除记录 
DELETE FROM user_tbl WHERE name = '李四' ;

# 添加栏位 
ALTER TABLE user_tbl ADD email VARCHAR(40);

# 更新结构 
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;

# 更名栏位 
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;

# 删除栏位 
ALTER TABLE user_tbl DROP COLUMN email;

# 表格更名 
ALTER TABLE user_tbl RENAME TO backup_tbl;

# 删除表格 
DROP TABLE IF EXISTS backup_tbl;

登陆pgAdmin

Deepin V20 Linux 安装 postgresql完美方案_第1张图片

登陆过程中遇到Postgresql: password authentication failed for user “postgres”,使用命令重置密码:

you have root access on the box you can do:

sudo -u postgres psql

Inside the psql shell you can give the DB user postgres a password:

ALTER USER postgres PASSWORD 'newPassword';

 

你可能感兴趣的:(Deepin,Linux,平台开发者)