Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98409 > unrolled thread

Problems connecting to PostgreSQL

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-11-08 00:40 +0100
Last post2015-11-09 13:59 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Problems connecting to PostgreSQL Cecil Westerhof <Cecil@decebal.nl> - 2015-11-08 00:40 +0100
    Re: Problems connecting to PostgreSQL Chris Angelico <rosuav@gmail.com> - 2015-11-08 10:54 +1100
    Re: Problems connecting to PostgreSQL Chris Warrick <kwpolska@gmail.com> - 2015-11-08 09:36 +0100
      Re: Problems connecting to PostgreSQL Cecil Westerhof <Cecil@decebal.nl> - 2015-11-09 13:59 +0100

#98409 — Problems connecting to PostgreSQL

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-11-08 00:40 +0100
SubjectProblems connecting to PostgreSQL
Message-ID<87wptt5qg4.fsf@Equus.decebal.nl>
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.

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 = psycopg2.connect(database = postgres_database, user = 'stressTest')
I get:
    psycopg2.OperationalError: FATAL:  Peer authentication failed for user "stressTest"

What do I need to do to get things working?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#98411

FromChris Angelico <rosuav@gmail.com>
Date2015-11-08 10:54 +1100
Message-ID<mailman.113.1446940500.16136.python-list@python.org>
In reply to#98409
On Sun, Nov 8, 2015 at 10:40 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> 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.
>

You might need to become root before you can become another user - it
depends on your sudoers file. Try this:

sudo sudo -u postgres createuser stressTest

Note that this isn't a Python question, but a PostgreSQL and system
administration one. You may find good results by searching the web for
your issue and omitting all reference to Python.

ChrisA

[toc] | [prev] | [next] | [standalone]


#98428

FromChris Warrick <kwpolska@gmail.com>
Date2015-11-08 09:36 +0100
Message-ID<mailman.119.1446971807.16136.python-list@python.org>
In reply to#98409
On 8 November 2015 at 00:40, Cecil Westerhof <Cecil@decebal.nl> 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 — and it comes from sudo,
postgres doesn’t 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 = psycopg2.connect(database = postgres_database, user = 'stressTest')
> I get:
>     psycopg2.OperationalError: FATAL:  Peer authentication failed for user "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=# ALTER ROLE stressTest WITH PASSWORD 'swordfish';
    postgres=# \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 = psycopg2.connect(database='stressTest', user='stressTest',
password='swordfish', host='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)

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

[toc] | [prev] | [next] | [standalone]


#98519

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-11-09 13:59 +0100
Message-ID<87pozj5nxm.fsf@Equus.decebal.nl>
In reply to#98428
On Sunday  8 Nov 2015 09:36 CET, Chris Warrick wrote:

> On 8 November 2015 at 00:40, Cecil Westerhof <Cecil@decebal.nl> 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 — and it comes from sudo,
> postgres doesn’t 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 =
>> psycopg2.connect(database = postgres_database, user = 'stressTest')
>> I get: psycopg2.OperationalError: FATAL: Peer authentication failed
>> for user "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=# ALTER ROLE stressTest WITH PASSWORD 'swordfish';
> postgres=# \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 = psycopg2.connect(database='stressTest', user='stressTest',
> password='swordfish', host='localhost')

Thanks. The file to edit on my system is:
    /var/lib/pgsql/data/pg_hba.conf
and it also looks like I cannot use mixed case. But at the moment I
have it working. I will delve deeper into it later.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web