如何在Ubuntu 20.04上安装PostgreSQL [快速入门]

介绍 (Introduction)

PostgreSQL, or Postgres, is a relational database management system that provides an implementation of the SQL querying language. It’s standards-compliant and has many advanced features like reliable transactions and concurrency without read locks.

PostgreSQL或Postgres是一种关系数据库管理系统,提供SQL查询语言的实现。 它符合标准,并具有许多高级功能,例如可靠的事务处理和并发,没有读锁。

This guide demonstrates how to quickly get Postgres up and running on an Ubuntu 20.04 server, from installing PostgreSQL to setting up a new user and database. If you’d prefer a more in-depth tutorial on installing and managing a PostgreSQL database, see How To Install and Use PostgreSQL on Ubuntu 20.04.

本指南演示了如何快速安装Postgres并在Ubuntu 20.04服务器上运行,从安装PostgreSQL到设置新用户和数据库。 如果您希望获得有关安装和管理PostgreSQL数据库的更深入的教程,请参见如何在Ubuntu 20.04上安装和使用PostgreSQL

先决条件 (Prerequisites)

To follow along with this tutorial, you will need one Ubuntu 20.04 server that has been configured by following our Initial Server Setup for Ubuntu 20.04 guide. After completing this prerequisite tutorial, your server should have a non-root user with sudo permissions and a basic firewall.

要继续本教程,您将需要按照我们的《 Ubuntu 20.04初始服务器设置》指南配置一台Ubuntu 20.04服务器。 完成此先决条件教程之后,您的服务器应具有具有sudo权限的非root用户和基本防火墙。

第1步-安装PostgreSQL (Step 1 — Installing PostgreSQL)

To install PostgreSQL, first refresh your server’s local package index:

要安装PostgreSQL,首先刷新服务器的本地软件包索引:

  • sudo apt update

    sudo apt更新

Then, install the Postgres package along with a -contrib package that adds some additional utilities and functionality:

然后,安装Postgres软件包以及-contrib软件包,该软件包添加了一些其他实用程序和功能:

  • sudo apt install postgresql postgresql-contrib

    sudo apt安装postgresql postgresql-contrib

第2步-使用PostgreSQL角色和数据库 (Step 2 — Using PostgreSQL Roles and Databases)

By default, Postgres uses a concept called “roles” to handle authentication and authorization. These are, in some ways, similar to regular Unix-style users and groups.

默认情况下,Postgres使用称为“角色”的概念来处理身份验证和授权。 在某些方面,它们类似于常规的Unix风格的用户和组。

Upon installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

安装后,Postgres设置为使用身份认证,这意味着它将Postgres角色与匹配的Unix / Linux系统帐户相关联。 如果Postgres中存在角色,则具有相同名称的Unix / Linux用户名可以作为该角色登录。

The installation procedure created a user account called postgres that is associated with the default Postgres role. There are a few ways to utilize this account to access Postgres. One way is to switch over to the postgres account on your server by typing:

安装过程创建了一个名为postgres的用户帐户,该帐户与默认的Postgres角色相关联。 有几种方法可以使用该帐户访问Postgres。 一种方法是通过键入以下命令切换到服务器上的postgres帐户:

  • sudo -i -u postgres

    须藤-i -u postgres

Then you can access the Postgres prompt by typing:

然后,您可以输入以下内容来访问Postgres提示符:

  • psql

    psql

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

这将使您登录到PostgreSQL提示符,从这里您可以立即自由地与数据库管理系统进行交互。

To exit out of the PostgreSQL prompt, run the following:

要退出PostgreSQL提示符,请运行以下命令:

  • \q

    \ q

This will bring you back to the postgres Linux command prompt. To return to your regular system user, run the exit command:

这将带您回到postgres Linux命令提示符。 要返回常规系统用户,请运行exit命令:

  • exit

    出口

Another way to connect to the Postgres prompt is to run the psql command as the postgres account directly with sudo:

连接到Postgres提示符的另一种方法是直接使用sudo作为postgres帐户运行psql命令:

  • sudo -u postgres psql

    须藤-u postgres psql

This will log you directly into Postgres without the intermediary bash shell in between.

这将使您直接登录到Postgres,而无需中间的bash shell。

Again, you can exit the interactive Postgres session by typing:

同样,您可以通过键入以下命令退出交互式Postgres会话:

  • \q

    \ q

第3步-创建新角色 (Step 3 — Creating a New Role)

If you are logged in as the postgres account, you can create a new role by typing:

如果您以postgres帐户登录,则可以通过键入以下内容来创建新角色:

  • createuser --interactive

    createuser --interactive

If, instead, you prefer to use sudo for each command without switching from your normal account, type:

相反,如果您希望对每个命令使用sudo而不用从普通帐户切换,请键入:

  • sudo -u postgres createuser --interactive

    sudo -u postgres createuser --interactive

Either way, the script will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user to your specifications.

无论哪种方式,脚本都会提示您一些选择,并根据您的响应执行正确的Postgres命令以创建符合您要求的用户。


   
   
Output
Enter name of role to add: sammy Shall the new role be a superuser? (y/n) y

步骤4 —创建一个新数据库 (Step 4 — Creating a New Database)

Another assumption that the Postgres authentication system makes by default is that for any role used to log in, that role will have a database with the same name which it can access.

默认情况下,Postgres身份验证系统做出的另一个假设是,对于用于登录的任何角色,该角色将具有一个其名称可以访问的数据库。

This means that if the user you created in the last section is called sammy, that role will attempt to connect to a database which is also called “sammy” by default. You can create the appropriate database with the createdb command.

这意味着,如果您在上一节中创建的用户称为sammy ,则该角色将尝试连接到默认情况下也称为“ sammy”的数据库。 您可以使用createdb命令创建适当的数据库。

If you are logged in as the postgres account, you would type something like:

如果您以postgres帐户登录,则应输入以下内容:

  • createdb sammy

    createdb sammy

If, instead, you prefer to use sudo for each command without switching from your normal account, you would type:

相反,如果您希望对每个命令使用sudo而不从普通帐户切换,则应输入:

  • sudo -u postgres createdb sammy

    须藤-u postgres createdb sammy

第5步-使用新角色打开Postgres提示 (Step 5 — Opening a Postgres Prompt with the New Role)

To log in with ident based authentication, you’ll need a Linux user with the same name as your Postgres role and database.

要使用基于身份的ident验证登录,您需要一个Linux用户,该用户名称与Postgres角色和数据库相同。

If you don’t have a matching Linux user available, you can create one with the adduser command. You will have to do this from your non-root account with sudo privileges (meaning, not logged in as the postgres user):

如果没有可用的匹配Linux用户,则可以使用adduser命令创建一个。 您将必须使用sudo特权(意味着,不是以postgres用户身份登录)从非root帐户执行此操作:

  • sudo adduser sammy

    sudo adduser sammy

Once this new account is available, you can either switch over and connect to the database by typing:

一旦此新帐户可用,您可以通过键入以下内容来切换并连接到数据库:

  • sudo -i -u sammy

    须藤-i -u sammy

  • psql

    psql

Or, you can do this inline:

或者,您可以内联执行此操作:

  • sudo -u sammy psql

    须藤-u sammy psql

This command will log you in automatically, assuming that all of the components have been properly configured.

假设所有组件均已正确配置,此命令将自动登录。

If you want your user to connect to a different database, you can do so by specifying the database like this:

如果希望用户连接到其他数据库,可以通过指定数据库来做到这一点:

  • psql -d postgres

    psql -d postgres

Once logged in, you can get check your current connection information by typing:

登录后,您可以通过输入以下内容来检查当前的连接信息:

  • \conninfo

    \ conninfo

   
   
Output
You are connected to database "sammy" as user "sammy" via socket in "/var/run/postgresql" at port "5432".

结论 (Conclusion)

You are now set up with PostgreSQL on your Ubuntu 20.04 server. If you’d like to learn more about Postgres and how to use it, we encourage you to check out the following guides:

现在,您已在Ubuntu 20.04服务器上使用PostgreSQL进行设置。 如果您想了解有关Postgres及其使用方法的更多信息,我们建议您阅读以下指南:

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值