Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Warrick Newsgroups: comp.lang.python Subject: Re: Problems connecting to PostgreSQL Date: Sun, 8 Nov 2015 09:36:38 +0100 Lines: 59 Message-ID: References: <87wptt5qg4.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de BRC22xbtA59LkGiiFB58jA/G2nD49necprOAewlTiVww== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'configure': 0.04; 'matches': 0.07; 'postgresql': 0.07; 'cc:addr:python-list': 0.09; 'alter': 0.09; 'created,': 0.09; 'postgres': 0.09; '\xe2\x80\x94': 0.09; 'user.': 0.15; 'also:': 0.16; 'conn': 0.16; 'doesn\xe2\x80\x99t': 0.16; 'ipv6': 0.16; 'md5': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'sudo': 0.16; 'used:': 0.16; 'wrote:': 0.16; 'settings': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'permission': 0.20; 'denied': 0.22; 'role,': 0.22; 'cc:no real name:2**0': 0.22; 'this:': 0.23; 'unix': 0.24; 'header:In-Reply- To:1': 0.24; 'chris': 0.26; 'followed': 0.27; 'message- id:@mail.gmail.com': 0.27; 'host': 0.28; "skip:' 10": 0.28; 'peer': 0.29; 'silence': 0.29; 'checks': 0.30; 'role': 0.32; 'worked': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'things': 0.38; 'received:209': 0.38; 'skip:p 20': 0.38; 'your': 0.60; 'skip:u 10': 0.61; 'default': 0.61; 'different': 0.63; 'cecil': 0.84; 'skip:/ 30': 0.84; 'westerhof': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=IyqrBmZka1sAI8bjOR5A7d1F0unfR8EAtHYfC8LDvjE=; b=TNWYnpyYAk2N5FGYhxJ752FA+PY+m9O3/QQDx9Yd4E9xGXQ/tdRTlt8nCmkmhd4pqW AvAoAuIyXN6JHJY/EarmFhTPRj/KSNqlS89VRYi13JeJhi44xEReNFr0z4zhktC1yFPf uZthjo9+y4pggiyLCflEN3pQkqHgn6gegRDdaoso2wG8AmXqjCsTqv55ZFnOowx4qykk BvHRCqoVFUcURMWgQX8MDEnlLPBh8quudZiTWLXwTL3LChdpikWnO49XLuhLP5IuG3Bz XHZ1HNswRWZ7ZaPUlYyPbGCD34aJe++a6x9/zvwmcOJW6pbW2km4JlA6nb6CgeSBYmrw Kldw== X-Received: by 10.31.171.140 with SMTP id u134mr22912130vke.30.1446971798682; Sun, 08 Nov 2015 00:36:38 -0800 (PST) In-Reply-To: <87wptt5qg4.fsf@Equus.decebal.nl> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98428 On 8 November 2015 at 00:40, Cecil Westerhof wrote: > I followed http://zetcode.com/db/postgresqlpythontutorial/. > > I used: > sudo -u postgres createuser stressTest > this create the role, but also gave: > could not change directory to "/root": Permission denied > and I did not get the questions. This is not an error, just a warning =E2=80=94 and it comes from sudo, postgres doesn=E2=80=99t care. To silence it, you need to work from a different directory than /root. The commands actually worked just fine. > Then I used: > sudo -u postgres createdb stressTest -O stressTest > This gave also: > could not change directory to "/root": Permission denied > > The database is created, but when I execute: > conn =3D psycopg2.connect(database =3D postgres_database, user =3D 's= tressTest') > I get: > psycopg2.OperationalError: FATAL: Peer authentication failed for use= r "stressTest" > > What do I need to do to get things working? You need to configure your PostgreSQL database to use md5 authentication, and set a password for your user. # cd / # sudo -u postgres psql postgres=3D# ALTER ROLE stressTest WITH PASSWORD 'swordfish'; postgres=3D# \q # vim /var/lib/postgres/data/pg_hba.conf Change host settings to look like this: # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 Then you can connect using: conn =3D psycopg2.connect(database=3D'stressTest', user=3D'stressTest', password=3D'swordfish', host=3D'localhost') Documentation: http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html http://www.postgresql.org/docs/current/static/auth-methods.html http://www.postgresql.org/docs/current/static/sql-alterrole.html (basically, the default peer authentication checks your Unix user name to see if it matches 'stressTest', and fails) --=20 Chris Warrick PGP: 5EAAEA16