如何在macOS上安装PostgreSQL

The following instructions to install PostgreSQL are based on macOS.

以下有关安装PostgreSQL说明基于macOS。

For Windows and Linux, go to https://www.postgresql.org/download/ and choose your package.

对于Windows和Linux,请访问https://www.postgresql.org/download/,然后选择您的软件包。

Also search “how to install postgres on windows” or “how to install postgres on your linux distribution” if you’re using other platforms.

如果使用其他平台,也请搜索“如何在Windows上安装postgres”或“如何在your linux distribution上安装postgres”。

It should not differ a lot, especially past the installation phase.

它应该相差不大,尤其是在安装阶段之后。

On macOS we’ll use Homebrew. If you don’t have Homebrew installed yet, go to https://brew.sh/ and follow the instructions there.

在macOS上,我们将使用Homebrew 。 如果尚未安装Homebrew,请转到https://brew.sh/并按照其中的说明进行操作。

Once you are done, get back and in the command line run:

完成后,返回并在命令行中运行:

brew install postgresql

and after it finished, run:

完成后,运行:

brew services start postgresql

to start PostgreSQL as a daemon, which means it will keep running in the background, listening for connections.

将PostgreSQL作为守护程序启动 ,这意味着它将继续在后台运行,监听连接。

Using Homebrew has the great advantage that any update can be installed by running

使用Homebrew的巨大优势是可以通过运行以下命令安装任何更新

brew upgrade postgresql
brew postgresql-upgrade-database
brew services restart postgresql

postgresql is the more complex to pronounce name of PostgreSQL, but they are the same thing. It just embeds SQL in the name. What’s SQL? SQL, pronouced “sequel”, means Structured Query Language, and it’s a special language we use to interact with a relational database.

postgresql是PostgreSQL名称的更复杂的发音,但是它们是同一回事。 它只是将SQL嵌入名称中。 什么是SQL ? SQL,被称为“续集”,表示结构化查询语言,它是一种用于与关系数据库进行交互的特殊语言。

If you’re new to database, it’s a lot of new terms for you! Basically a relational database organizes the data into tables, and provides a way to insert and extract data from those tables. That’s SQL.

如果您不熟悉数据库,那么这对您来说是很多新术语! 基本上,关系数据库将数据组织到表中 ,并提供一种从这些表中插入和提取数据的方法。 那是SQL。

And we’re going to use it soon.

我们将很快使用它。

Right after we log in to PostgreSQL!

在我们登录PostgreSQL之后!

Go back to the command line, and type

返回命令行,然后键入

psql postgres

using psql

This will give you access to the postgres database, which is created by default, with your macOS username. Homebrew automatically created your user at installation.

这将使您可以访问使用默认MacOS用户名创建的postgres数据库。 Homebrew在安装时会自动创建您的用户。

Now that we are into the psql application, we can create a new database:

现在我们进入了psql应用程序,我们可以创建一个新的数据库:

CREATE DATABASE test;

Don’t forget the semicolon ;, because it’s needed by SQL otherwise the command will not run.

不要忘记分号; ,因为SQL需要它,否则该命令将不会运行。

Now in a new line, we switch to this database using

现在在新行中,我们使用

\c test

The prompt will tell something like this:

提示将显示以下内容:

You are now connected to database "test" as user "flaviocopes"..

You are now connected to database "test" as user "flaviocopes".

Now we’re going to create a new table.

现在,我们将创建一个新表。

Use this syntax:

使用以下语法:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) UNIQUE NOT NULL,
  password VARCHAR(255) NOT NULL
);

Now if you run this, and no error shows up, you will have the table in the system.

现在,如果运行此命令,并且没有出现错误,则系统中将包含该表。

You can see it by running the command

您可以通过运行命令来查看它

\dt

which will show you the database tables:

这将向您显示数据库表:

If you did any error, you can delete the table by running the command

如果发生任何错误,可以通过运行以下命令删除表

DROP TABLE users

To finally quit psql, run

最后退出psql ,运行

\q

or just type quit.

或者只是键入quit

Now that you know how things work under the hood, I will show you an easier way to work with PostgreSQL, and other databases too: the TablePlus app. It works on macOS, Windows and Linux.

既然您知道事情的幕后知识 ,我将向您展示一种使用PostgreSQL和其他数据库的更简单方法: TablePlus应用程序 。 它适用于macOS,Windows和Linux。

Connect to the database:

连接到数据库:

specifying the test database name:

指定test数据库名称:

In addition to be able to inspect tables with a Graphical User Interface:

除了能够使用图形用户界面检查表之外:

It also allows us to work with SQL queries, very easily:

它还使我们可以非常轻松地使用SQL查询:

翻译自: https://flaviocopes.com/postgres-how-to-install/

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值