Используйте базу данных postgresql в хосте Windows 10 от w7 virtualmachine - PullRequest
0 голосов
/ 11 июня 2019

Так что я пытаюсь получить доступ к базе данных postgresql в виртуальной машине с Windows 7, но кажется, что у него возникли некоторые проблемы с сетью из-за того, что я не могу получить доступ изнутри виртуальной машины, но да извне

Main.py

import psycopg2

from configparser import ConfigParser

def config(filename='database.ini', section='postgresql'):
    # create a parser
    parser = ConfigParser()
    # read config file
    parser.read(filename)

    # get section, default to postgresql
    db = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            db[param[0]] = param[1]
    else:
        raise Exception('Section {0} not found in the {1} file'.format(section, filename))

    return db

def connect():
    """ Connect to the PostgreSQL database server """
    conn = None
    try:
        # read connection parameters
        params = config()

        # connect to the PostgreSQL server
        print('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)

        # create a cursor
        cur = conn.cursor()

        # execute a statement
        print('PostgreSQL database version:')
        cur.execute('SELECT version()')

        # display the PostgreSQL database server version
        db_version = cur.fetchone()
        print(db_version)

        # close the communication with the PostgreSQL
        cur.close()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        if conn is not None:
            conn.close()
            print('Database connection closed.')


if __name__ == '__main__':
    connect()

database.ini

[postgresql]
host=127.0.0.1
database=database_name
user=postgres
password=password

запуск кода с моего компьютера =

  C:\Users\localhost\AppData\Local\Programs\Python\Python37\python.exe "C:/Users/localhost/PycharmProjects/Scripts Python/VM_Manager/Database/Main.py"
Connecting to the PostgreSQL database...
PostgreSQL database version:
('PostgreSQL 10.8, compiled by Visual C++ build 1800, 64-bit',)
Database connection closed.

Process finished with exit code 0

внутри виртуальной машины Windows 7

Connecting to the PostgreSQL database...
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "127.0.0.1" and accepting
    TCP/IP connections on port 5432?

Я спрашиваю в vmware ис помощью nat вы можете локально получить доступ ко всем ресурсам, так как localhost одинаков для каждой сети, поэтому мой вопрос в том, что я могу сделать плохо, не имея доступа к виртуальной машине к базе данных?если это то же самое?

...