使用psycopg2

时间:2016-10-08 06:07:47

标签: python postgresql python-3.x

  

psycopg2.OperationalError:无法连接到服务器:拒绝连接

服务器是否在主机上运行" 45.32.1XX.2XX"并接受端口5432上的TCP / IP连接?

在这里,我打开了我的插座。

tcp        0      0 127.0.0.1:5432          0.0.0.0:*  LISTEN      11516/postgres                
tcp6       0      0 ::1:5432                :::*       LISTEN      11516/postgres

我用Google搜索我应该修改此pg_hba.conf,但在我的postgresql根文件中,我根本找不到此文件。

此外,我已成功连接另一台服务器。

感谢。

在这里,我修改了pg_hba.conf,更新了此host all all 218.3.A.B trust并重新加载。但它也没有工作。

3 个答案:

答案 0 :(得分:4)

您的netstat输出显示postgres正在监听127.0.0.1,但您的错误表明您正在尝试连接到45.32.1XX.2XX。我很确定你已经诊断出你的问题。

您需要修改listen_addresses设置postgresql.conf文件( pg_hba.conf)。 postgresql.conf文件位于postgresql数据目录中,通常类似于/var/lib/postgresql/data/var/lib/pgsql/data

listen_addresses参数记录在案here

答案 1 :(得分:1)

[  391.693385] general protection fault: 0000 [#1] SMP
[  391.694435] Modules linked in: xt_CHECKSUM iptable_mangle ipt_MASQUERADE nf_nat_masquerade_ipv4 iptable_nat nf_nat_ipv4 nf_nat nf_conntrack_ipv4 nf_defrag_ipv4 xt_conntrack nf_conntrack ipt_REJECT nf_reject_ipv4 xt_tcpudp bridge stp llc ebtable_filter ebtables ip6table_filter ip6_tables iptable_filter ip_tables x_tables vmw_vsock_vmci_transport vsock kvm_intel kvm irqbypass vmw_balloon input_leds joydev serio_raw shpchp vmw_vmci i2c_piix4 mac_hid ib_iser rdma_cm iw_cm ib_cm ib_sa ib_mad ib_core ib_addr iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi autofs4 btrfs raid10 raid456 async_raid6_recov async_memcpy async_pq async_xor async_tx xor raid6_pq libcrc32c raid1 raid0 multipath linear crct10dif_pclmul crc32_pclmul ghash_clmulni_intel aesni_intel aes_x86_64 lrw gf128mul glue_helper vmwgfx ablk_helper
[  391.702930]  cryptd ttm drm_kms_helper syscopyarea psmouse sysfillrect pata_acpi sysimgblt mptspi fb_sys_fops mptscsih drm mptbase vmxnet3 scsi_transport_spi floppy fjes
[  391.705034] CPU: 3 PID: 1716 Comm: java Not tainted 4.4.131 #4
[  391.706080] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/28/2017
[  391.708180] task: ffff88042607c600 ti: ffff8804292b8000 task.ti: ffff8804292b8000
[  391.709244] RIP: 0010:[<ffffffff811a34dc>]  [<ffffffff811a34dc>] put_compound_page+0x5c/0x1b0
[  391.710358] RSP: 0000:ffff8804292bbcc8  EFLAGS: 00210202
[  391.711439] RAX: 00d0a78b4c535441 RBX: ffffffff810dc4f9 RCX: 000507e043713000
[  391.712523] RDX: ffff8804292bbd44 RSI: 000507e043713000 RDI: ffffffff810dc4f9
[  391.713586] RBP: ffff8804292bbcd8 R08: ffff880002213cf0 R09: 00003ffffffff000
[  391.714653] R10: 0000000000000080 R11: 0000000000000000 R12: 00d0a78b4c535440
[  391.715712] R13: 0000160000000000 R14: ffff8804292bbd88 R15: ffffffff810dc4f9
[  391.716764] FS:  00007fb138d5b700(0000) GS:ffff88042d6c0000(0000) knlGS:0000000000000000
[  391.717829] CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
[  391.718877] CR2: 0000000000000000 CR3: 00000000351d1000 CR4: 00000000001606f0
[  391.719972] Stack:
[  391.720993]  ffffffff810dc4f9 ffff880000000000 ffff8804292bbcf0 ffffffff811a364d
[  391.722055]  ffff8804292bbdc8 ffff8804292bbdf8 ffffffff8102e21e ffff8804292bbd48
[  391.723122]  0000000000000000 ffff88042607c600 ffff880429e6ac00 ffff880425e9f388
[  391.724165] Call Trace:
[  391.725190]  [<ffffffff810dc4f9>] ? vprintk_default+0x29/0x40
[  391.726222]  [<ffffffff811a364d>] put_page+0x1d/0x50
[  391.727259]  [<ffffffff8102e21e>] clear_process_cache+0x11e/0x1f0
[  391.728298]  [<ffffffff810dc4f9>] ? vprintk_default+0x29/0x40
[  391.729318]  [<ffffffff811918d0>] ? printk+0x5a/0x76
[  391.730328]  [<ffffffff8102e93d>] do_signal+0x20d/0x770
[  391.731310]  [<ffffffff81193459>] ? unlock_page+0x69/0x70
[  391.732297]  [<ffffffff811972c0>] ? __probe_kernel_read+0x40/0x90
[  391.733271]  [<ffffffff8106d3c3>] ? bad_area+0x43/0x50
[  391.734220]  [<ffffffff810034fc>] exit_to_usermode_loop+0x8c/0xd0
[  391.735143]  [<ffffffff81003c26>] prepare_exit_to_usermode+0x26/0x30
[  391.736062]  [<ffffffff8185184e>] retint_user+0x8/0x34
[  391.736941] Code: ff 5b 41 5c 5d c3 48 89 df e8 01 f6 ff ff 48 89 df 31 f6 e8 17 76 ff ff 5b 41 5c 5d c3 48 8b 47 20 4c 8d 60 ff a8 01 4c 0f 44 e7 <41> f6 44 24 08 01 74 08 49 8b 04 24 a8 80 74 1a 48 8b 43 20 a8
[  391.739698] RIP  [<ffffffff811a34dc>] put_compound_page+0x5c/0x1b0
[  391.740571]  RSP <ffff8804292bbcc8>

答案 2 :(得分:0)

也检查端口。

也许您的应用程序没有在默认 5432 上运行。

端口配置在 postgresql.conf 文件中