Impossible de se connecter à une base de données Postgresql

Bonjour à tous,
Je développe actuellement sur mac et n'arrive pas en php à me connecter à ma BDD
Le message d'erreur est le suivant :

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

Alors j'ai tout essayé (modifier les fichiers postgresql.conf et pg_hba.conf et redémarré le serveur postgresql) et fait plusieurs manips trouvées sur internet...

Je vous mets également mon script php au cas où :

<?php
$db = pg_connect("host=localhost port=5432 dbname=bd***** user=postgres password=******");
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>

Merci à ceux qui essaieront de me débloquer !

14 réponses

  1. re,

    on va bien finir par trouver une solution ...

    tu arrive à te connecter depuis un terminal ?

    - stop PGAdmin
    - redémarre postgresql
    - essai de te connecter depuis un terminal

    psql -d tabase -h localhost -p 5432 -U postgres


    - essais de voir ce qui se passe dans les log, chez c'est dans
     var -> log -> postgresql -> postgresql-12-main.log


    Je n'ai pas mis le chemin des logs avec des / car le forum bloque mon message.

    Puis essais de nouveau en te connectant avec PHP et vérifie de nouveau les logs
    0
    1. J'ai cherché une correspondance pour mac mais j'ai pas trouvé il m'as renvoyé ça

      netstat: lnt: unknown or uninstrumented protocol
      0
      1. ok toujours pour debugger

        netstat -plnt

        0
        1. Effectivement

            503  9793     1   0  3:49   ??         0:01.00 /Library/PostgreSQL/12/bin/postmaster -D/Library/PostgreSQL/12/data
          501 32915 32589 0 3:10 ttys001 0:00.01 grep -i postgresql
          0
          1. c'est peut-être différent sur mac

            ps -ef | grep -i postgresql 

            0
            1. Alors je pense qu'il y a un problème puisqu'il me renvoie

              ps: No user named 'x'
              0
              1. Tu as peut être 2 postgres en fonction ?

                Sa donne quoi ça :

                ps -aux | grep -i postgresql

                0
                1. Si ça peut aider j'ai mis ce code :

                  pg_ctl -D /usr/local/var/postgres start


                  et j'ai obtenu le résultat suivant :

                  waiting for server to start....2020-07-31 14:28:06.174 CEST [31379] LOG:  starting PostgreSQL 12.3 on x86_64-apple-darwin19.4.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.59), 64-bit
                  2020-07-31 14:28:06.175 CEST [31379] LOG: could not bind IPv6 address "::1": Address already in use
                  2020-07-31 14:28:06.175 CEST [31379] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
                  2020-07-31 14:28:06.175 CEST [31379] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
                  2020-07-31 14:28:06.175 CEST [31379] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
                  2020-07-31 14:28:06.175 CEST [31379] WARNING: could not create listen socket for "localhost"
                  2020-07-31 14:28:06.175 CEST [31379] FATAL: could not create any TCP/IP sockets
                  2020-07-31 14:28:06.175 CEST [31379] LOG: database system is shut down
                  stopped waiting
                  pg_ctl: could not start server
                  Examine the log output.
                  0
                  1. Salut,

                    Alors le plus étrange c'est que j'ai PGAdmin 4 d'ouvert et celui-ci fonctionne très bien en localhost puisque je peux créer des BDD, etc

                    J'ai vérifié dans postgresql.conf et j'avais bien ces deux lignes qui n'étaient pas commentées

                    listen_addresses = '*'	

                    port = 5432


                    Et dans le fichier pg_hba.conf voici ce qui ressort (j'ai juste mit trust partout pour la méthode)

                    # TYPE  DATABASE        USER            ADDRESS                 METHOD

                    # "local" is for Unix domain socket connections only
                    local all all trust
                    # IPv4 local connections:
                    host all all 127.0.0.1/32 trust
                    # IPv6 local connections:
                    host all all ::1/128 trust
                    # Allow replication connections from localhost, by a user with the
                    # replication privilege.
                    local replication all trust
                    host replication all 127.0.0.1/32 trust
                    host replication all ::1/128 trust


                    Sachant qu'effectivement je n'ai jamais réussit par le passé sur cet ordinateur à faire la connexion
                    0
                    1. Salut,

                      bon j'espérais voir autre autre chose dans netstat, mais il semblerait que tu as des connexions qui sont se font, tu as des autres applications qui utilisent postgresql de lancées ?

                      Tu as modifié quoi dans les fichiers postgresql.conf et pg_hba.conf ?

                      Postgresql a déjà fonctionné sur cet ordinateur ?

                      A+
                      0
                      1. il me retourne ça :

                        tcp6       0      0  localhost.postgresql   localhost.60419        ESTABLISHED
                        tcp6 0 0 localhost.60419 localhost.postgresql ESTABLISHED
                        tcp6 0 0 localhost.postgresql localhost.60402 ESTABLISHED
                        tcp6 0 0 localhost.60402 localhost.postgresql ESTABLISHED
                        tcp4 0 0 *.postgresql *.* LISTEN
                        tcp6 0 0 *.postgresql *.* LISTEN
                        0
                        1. ok et que retourne

                          netstat -a | grep -i postgresql

                          0
                          1. Alors pour lancer postgresql j'utilise dans le terminal :

                            brew services start postgresql


                            Et il me renvoie qu'il est déjà démarré :

                            Service `postgresql` already started, use `brew services restart postgresql` to restart.
                            0
                            1. Salut,

                              Je ne connais pas trop sur MAC, mais es-ce que postgresql est actif ?

                              dans un terminal, si tu tape:

                              psql


                              tu obtiens quoi ?
                              0