未加星标

How to install PostgreSQL 9.5 on Ubuntu 16.04

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

PostgreSQL is an open source relational database system that has been around for well over a decade and has proven to be a great all around storage choice when developing a web application.

In this guide we are going to walk through installing PostgreSQL 9.5 on Ubuntu 16.04 from scratch so that we can eventually start using it with a Go application, but you can follow along with this guide to set up Postgres for use with pretty much anything, including Rails, Django, or Go.

This post is part of the longer series, Using PostgreSQL with Golang , where we walk through all of the basics required to setup and start using PostgreSQL with Go.

If you want to stay up to date when new articles are released, or if you are interested in other articles about web development and Golang you shouldsign up for my mailing list. Not only will I let you know when I release a new post, but I'll also send you two free chapters from my upcoming book, Web Development with Go. Oh, and I promise I don't spam :)

Once we have Postgres setup we will also look at setting up the postgres role so that we can access it from any linux user using a password. This will make it easier for our applications to connect to the database without having to be run by the postgres system user.

Installation

The first thing we want to do before we start installing anything is to update apt-get . This will ensure that we are installing from an updated repository.

sudo apt-get update

After that finishes, we can go ahead and install all of the packages we are going to need.

sudo apt-get install postgresql postgresql-contrib

When you are prompted asking if you want to continue, type y and hit enter. This will go ahead and install both the postgres package and the postgres-contrib package, which adds some additional functionality to Postgres.

Congrats! You should now have Postgres installed, but by default you need to be logged into the postgres user account to access PostgreSQL. That likely isn't what you want - instead you probably want to set a password for postgres role and then use that password to log into Postgres from another user.

Setting up a password for the postgres role

By default, local connections to PostgreSQL use the peer authentication system. That means that instead of asking you for a password, they check to see if you are currently logged into a system user that matches the user name in Postgres.

We are going to change the way we do authentication and instead tell Postgres to use an encrypted password, but first we need to actually set a password for the postgres user. To do this we need to open up psql as the user postgres .

sudo -u postgres psql

You should see output that looks something like this:

could not change directory to "/root": Permission denied psql (9.5.4) Type "help" for help. postgres=#

Don't worry about the permission denied error. This isn't important right now, I promise.

Now that we are connected to Postgres, we want to change the password for the user postgres . Be sure to replace the xxxxxxx below with an actual password.

# Replace xxxxxxx with your own password ALTER USER postgres WITH ENCRYPTED PASSWORD 'xxxxxxx';

What we are doing here is telling Postgres that we want to update the user postgres by setting an encrypted password of xxxxxxx . If you did this correct, you should get the following output.

ALTER ROLE postgres=#

That is all we need to do inside of Postgres. Go ahead and quit by typing \q and then hitting enter.

Now that we have a password set for the postgres user we want to update Postgres to use this password. To do this we need to edit the pg_hba.conf file.

# Feel free to replace nano with an editor of your choice sudo nano /etc/postgresql/9.5/main/pg_hba.conf

Look for an uncommented line (a line that doesn't start with # ) that has the contents shown below. The spacing will be slightly different, but the words should be the same.

local all postgres peer

The last part of this line is what we want to change. This is what determines how we authenticate the postgres user when making a local connection. Instead of peer , which uses your system user name to authenticate you, we want to use md5 which uses an encrypted password for authentication. Replace the word peer with md5 .

local all postgres md5

Press ctrl+x to close nano, then hit y to confirm that you want to save, and hit enter to confirm the same file name as the original.

Now we need to restart Postgres so the changes take effect.

sudo service postgresql restart

Now if we want to connect to PostgreSQL we can using the postgres user and a password.

psql -U postgres # When prompted for your password, type it in Up Next

Want to stay up to date with new releases in the series?Sign up for my mailing listand I will notify you when I publish new articles. I will also send you TWO free chapters from my upcoming book, Web Development with Go, and other exclusive content that I only send to my mailing list.

Now that you have PostgreSQL 9.5 installed you are ready to start using it! Check out the next article in this series to learn how to do that - Creating databases and tables with PostgreSQL .

You can also check out an overview of the entire series, along with links to each article in the series, by going here - Using PostgreSQL with Golang .

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

主题: PostgreSQLSQLUbuntuDjangoRY
分页:12
转载请注明
本文标题:How to install PostgreSQL 9.5 on Ubuntu 16.04
本站链接:http://www.codesec.net/view/481196.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(58)