by Akul Tomar

通过Akul Tomar

如何开始使用PostgreSQL (How to get started with PostgreSQL)

PostgreSQL is an open source Relational Database Management System (RDBMS). In this article, I’ll provide an introduction to getting started with PostgreSQL. Here is what we’ll cover:

PostgreSQL是一个开源的关系数据库管理系统(RDBMS)。 在本文中,我将介绍PostgreSQL入门。 这是我们要介绍的内容:

  • Installation

    安装

  • Administration

    行政

  • Basic Database Operations基本数据库操作

安装 (Installation)

If you have homebrew installed on your system, you can run the command below on your terminal to quickly install PostgreSQL:

如果您的系统上安装了自制软件,则可以在终端上运行以下命令以快速安装PostgreSQL:

brew install postgresql

Others can download the latest version of PostgreSQL here and follow the installation steps.

其他人可以在这里下载最新版本的PostgreSQL并遵循安装步骤。

Once downloaded, to verify you’ve got PostgreSQL installed, run the following command to check your PostgreSQL version:

下载完成后,要验证是否已安装PostgreSQL,请运行以下命令以检查PostgreSQL版本:

postgres --version

行政 (Administration)

PostgreSQL can be administered from the command line using the psql utility, by running the command below:

可以使用以下命令通过psql实用工具从命令行管理PostgreSQL:

psql postgres

This should get your psql utility running. psql is PostgreSQL’s command line tool. While there are many third-party tools available for administering PostgreSQL databases, I haven’t felt the need to install any other tool yet. psql is pretty neat and works just fine.

这应该使您的psql实用程序运行。 psql是PostgreSQL命令行工具。 尽管有许多可用于管理PostgreSQL数据库的第三方工具,但我还没有必要安装任何其他工具。 psql非常简洁,可以正常工作。

To quit from the psql interface, you can type \q and you’re out.

要从psql界面退出,可以键入\q然后退出。

If you need help, type \help on your psql terminal. This will list all the available help options. You can type in \help [Command Name], in case you need help with a particular command. For example, typing in \help UPDATE from within psql will show you the syntax of the update option.

如果需要帮助, \help在psql终端上键入\help 。 这将列出所有可用的帮助选项。 如果需要有关特定命令的帮助,可以键入\help [Command Name] 。 例如,在psql输入\help UPDATE将显示更新选项的语法。

Description: update rows of a table[ WITH [ RECURSIVE ] with_query [, ...] ]UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]    SET { column_name = { expression | DEFAULT } |          ( column_name [, ...] ) = ( { expression | DEFAULT } [, ...] ) |          ( column_name [, ...] ) = ( sub-SELECT )        } [, ...]    [ FROM from_list ]    [ WHERE condition | WHERE CURRENT OF cursor_name ]    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

If you’re a beginner, you may still not understand. A quick Google search will provide you examples of its use or you can always search the official psql documentation which will provide many examples.

如果您是初学者,您可能仍然不明白。 快速的Google搜索将为您提供其用法示例,或者您始终可以搜索将提供许多示例的官方psql文档 。

When you first install PostgreSQL, there are a few common administrative tasks that you’ll frequently perform.

首次安装PostgreSQL时,您会经常执行一些常见的管理任务。

The first thing would be to check for existing users and databases. Run the command below to list all databases:

第一件事是检查现有的用户和数据库。 运行以下命令以列出所有数据库:

\list or \l

In the figure above, you can see three default databases and a superuser akultomar that get created when you install PostgreSQL.

在上图中,您可以看到在安装PostgreSQL时创建的三个默认数据库和一个超级用户akultomar

To list all users, use the \du command. The attributes of the user tell us that they’re a Superuser.

要列出所有用户,请使用\du命令。 用户的属性告诉我们他们是超级用户。

基本数据库操作 (Basic Database Operations)

To perform basic database operations, you use the Structured Query Language (commonly known as SQL).

要执行基本的数据库操作,请使用结构化查询语言(通常称为SQL)。

建立资料库 (Create a database)

To create a database, you use the create database command. In the example below, we’ll create a database named riskzone.

要创建数据库,请使用create database命令。 在下面的示例中,我们将创建一个名为riskzone的数据库。

If you forget the semicolon at the end, the = sign at the postgres prompt is replaced with a - as in the figure below. This is basically an indication that you need to terminate your query. You’ll understand it’s significance when you actually start writing longer queries. For now just put a semi-colon to complete the SQL statement and hit return.

如果忘记了最后的分号,则在postgres提示符下的=符号将替换为- ,如下图所示。 这基本上表明您需要终止查询。 当您真正开始编写更长的查询时,您将了解它的重要性。 现在只需要用分号来完成SQL语句并按回车即可。

创建一个用户 (Create a user)

To create a user, you use the create user command. In the example below, we’ll create a user named no_one.

要创建用户,请使用create user命令。 在下面的示例中,我们将创建一个名为no_one的用户。

When you create a user, the message shown is CREATE ROLE. Users are roles with login rights. I have used them interchangeably. You’ll also notice that the Attributes column is empty for the user no_one. This means that the user no_one has no administrative permissions. They can only read data and cannot create another user or database.

创建用户时,显示的消息是CREATE ROLE 。 用户是具有登录权限的角色。 我已经交替使用了它们。 您还会注意到,用户no_one的Attributes no_one空。 这意味着用户no_one没有管理权限。 他们只能读取数据,不能创建其他用户或数据库。

You can set a password for your user. To a set password for an existing user, you need use the \password command below:

您可以为用户设置密码。 要为现有用户设置密码,您需要使用以下\password命令:

postgres=#\password no_one

To set a password when a user is created, the command below can be used:

要在创建用户时设置密码,可以使用以下命令:

postgres=#create user no_two with login password 'qwerty';

删除用户或数据库 (Delete a user or database)

The drop command can be used to delete a database or user, as in the commands below.

可以使用drop命令删除数据库或用户,如以下命令所示。

drop database <database_name>drop user <user_name>

This command needs to be used very carefully. Things dropped don’t come back unless you have a backup in place.

需要非常小心地使用此命令。 除非您有备份,否则掉线的事情不会回来。

If we run the \du and \l that we learned about earlier to display the list of users and databases respectively, we can see that our newly created no_one user and riskzone database.

如果运行我们先前了解的\du\l分别显示用户和数据库列表,则可以看到我们新创建的no_one用户和riskzone数据库。

When you specify psql postgres (without a username), it logs into the postgres database using the default superuser (akultomar in my case). To log into a database using a specific user, you can use the command below:

当指定psql postgres (不带用户名)时,它将使用默认的超级用户(在我的情况下为akultomar )登录到postgres数据库。 要使用特定用户登录数据库,可以使用以下命令:

psql [database_name] [user_name]

Let’s login to the riskzone database with the no_one user. Hit \q to quit from the earlier postgres database and then run the command below to log into riskzone with the user no_one.

让我们用no_one用户登录到riskzone数据库。 命中\q从早期的Postgres数据库退出,然后运行下面的命令登录到riskzone与用户no_one

I hoped you like the short introduction to PostgreSQL. I’ll be writing another article to help you understand roles better. If you’re new to SQL, my advice would be to practice as much as you can. Get your hands dirty and create your own little tables and practice.

我希望您喜欢PostgreSQL简短介绍。 我将写另一篇文章,以帮助您更好地理解角色。 如果您不熟悉SQL,我的建议是尽可能多地练习。 弄脏双手,创建自己的小桌子并练习。

翻译自: https://www.freecodecamp.org/news/how-to-get-started-with-postgresql-9d3bc1dd1b11/

如何开始使用PostgreSQL相关推荐

  1. Postgresql 日志收集

    PG安装完成后默认不会记录日志,必须修改对应的(${PGDATA}/postgresql.conf)配置才可以,这里只介绍常用的日志配置. 1.logging_collector = on/off - ...

  2. pg数据库开启远程连接_如何运行远程客户端连接postgresql数据库

    如何运行远程客户端连接 postgresql 数据库 前提条件是 2 个: 1 , pg_hba.conf 里面配置了运行远程客户机连接 pg_hba.conf 配置后需要重新加载 reload 生效 ...

  3. Postgresql:删除及查询字段中包含单引号的数据

    Postgresql:删除及查询字段中包含单引号的数据 1. 假设pg表t_info的属性att,值为固定的:'test' 2. 假设值为不固定的,'abcde' 参考 1. 假设pg表t_info的 ...

  4. postgresql Insert插入的几个报错

    postgresql Insert插入的几个报错 1. org.postgresql.util.PSQLException: 未设定参数值 2 的内容. 2. postgresql : column ...

  5. 【Postgresql】触发器某个字段更新时执行,行插入或更新执行

    [Postgresql]触发器某个字段更新时执行,行插入或更新执行 1. postgresql触发器 2. 触发器的创建及示例 1) 字段更新时,触发 2) 行插入或更新时,触发 3. 触发器的删除 ...

  6. PostgreSql、MySql字段值为空时取其他值语句

    PostgreSql: COALESCE(expr1,expr2) COALESCE函数是返回参数中的第一个非null的值,它要求参数中至少有一个是非null的,如果参数都是null会报错. sele ...

  7. Postgresql:INSERT INTO ON CONSTRAINT pk_key_id do nothing

    一.Postgresql在插入数据有主键或者唯一键冲突时,可以不做任何操作,也可以update 1. do nothing INSERT INTO t_test(id,name) VALUES (10 ...

  8. Redhat、centos安装配置postgresql

    一.安装postgresql 本文仅以 redhat,postgresql9.4为例,使用yum方式进行介绍. 官网:http://www.postgresql.org/download/linux/ ...

  9. postgresql 远程用户_liunx环境下安装PostgresSQL,开启远程连接

    准备工作 # 检查PostgreSQL 是否已经安装 [root@localhost ~] rpm -qa | grep postgres # 检查PostgreSQL 安装位置 [root@loca ...

  10. 多个网站共享一个mysql数据库_如何在多个Postgresql数据库之间共享表

    是的,模式是解决方案.使用单个Postgresql集群,使用单个数据库. 为所有应用用户创建一个组: CREATE ROLE app; 创建全局"应用程序"模式,其中所有全局共享应 ...

最新文章

  1. python-day05正则表达式
  2. cisco SMD 配置安装
  3. Ubuntu20.04中安装shutter
  4. 论大型信息系统集成项目的整体管理
  5. 北斗导航 | 从北一到北二再到北三,你应该知道的北斗导航系统
  6. 2008铁路旅客列车时刻表_天津到新沂汽车卧铺大巴车长途汽车发车时刻表
  7. html原文档流样式,html之样式
  8. DEBUG命令的应用
  9. SUPERSET使用笔记
  10. 对于IDEA 导入eclipse项目后 没有办法启动部署 以及javax.servlet.http不存在
  11. 数据结构与算法14-栈和队列练习题
  12. 自己写daemon守护进程
  13. K3Cloud BOS设计 值更新 字段拼接到文本字段
  14. mysql中flush用法,flush 的常规用法:
  15. 调查问卷怎么html做成链接,在问卷网上如何把问卷链接通过邮件的方式发给别人填写?...
  16. linux系统无法网上看视频文件,重橙网络:Flash Player 发布重要更新,Win7 以下/Linux/Mac 不再支持视频格式内容播放...
  17. python复习。知识点小记
  18. Lua最简单的入门教程
  19. 兼职跑网约车能赚钱吗?
  20. 关于 CST 和UTC时间的理解

热门文章

  1. devServer proxy跨域 设置代理 proxy
  2. 微信小程序用户未授权bug解决方法,微信小程序获取用户信息失败解决方法
  3. iOS超全开源框架、项目和学习资料汇总:UI篇
  4. centos7下Gitlab+Jenkins部署持续集成CI环境
  5. datatable无法设置横向滚动条(设置无效)
  6. 微软在C# 8中引入预览版可空引用类型
  7. Ubuntu14.04 Mininet中将Openvswitch升级步骤
  8. 如何创建.gitignore文件,忽略git不必要提交的文件
  9. IE的安全性设定增加“我的电脑”的安全性设定
  10. frame,iframe,frameset之间的关系与区别