Psql ошибка не удалось подключиться к серверу connection refused

I have a problem to connect server for Postgres after I updated my windows.Before I update there is no problem to open the database. My database in Postgres also gone. When I want to create my new database it show this error:

Unable to connect to server: could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host «localhost» (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host «localhost» (127.0.0.1) and accepting TCP/IP connections on port 5432?

a_horse_with_no_name's user avatar

asked Nov 10, 2016 at 16:23

SySyBy's user avatar

6

On windows, Just go to the ‘Services’. Start/Restart the postgresql-X64 service. It worked for me as my service was in stopped state somehow.

answered Feb 23, 2020 at 7:06

gagan chhabra's user avatar

gagan chhabragagan chhabra

1,3451 gold badge8 silver badges9 bronze badges

4

There are two items to configure if your server isn’t on localhost:

  • find your postgresql.conf and add your server’s public IP address to the end of the setting listen_addresses (separate multiple entries by commas); uncomment the line if it is commented out (e.g. with ‘#’)
  • add a line to pg_hba.conf containing your client’s IP address — you may copy the line containing 127.0.0.1 and change only the IP address

On Ubuntu, these files are in /etc/postgresql/<version>/main/.

answered Mar 14, 2017 at 1:12

Franc Drobnič's user avatar

1

In my case I couldnt’ open the pgAdmin4, for some reason. I use Postgresql 10 and pgAdmin4

The port in the postgresql.conf was not the same as in the pgAdmin4 —> postgreSQL 10 —> properties —> Connection —> port.

I fixed it and it worked. Check if those 2 are in line.

answered Sep 12, 2020 at 13:55

Andreas Alamanos's user avatar

2

First press win key+R
Search for services.msc
A window will open in that find postgresql-x64-13 and open that, in that tab click start option
For me its works perfectly.

answered May 31, 2021 at 5:03

VADHAN's user avatar

VADHANVADHAN

2313 silver badges2 bronze badges

0

  1. Go to PgAdmin
  2. Right click on PostgreSQL
    3.Choose properties
  3. At the top, select connection
  4. Try changing the port from 5433 to 5432 or vice versa.

And re-enter.

answered May 15, 2021 at 17:27

Никита Мельников's user avatar

1

Faced this problem immediately after installing on Windows. At startup the pgAdmin gave this error which means that the server is not running. For me the solution was: Start -> Control panel -> Administration -> Services -> postgresql-x64-12 — start or restart

answered Aug 7, 2020 at 21:49

Tim  Uzlov's user avatar

Tim UzlovTim Uzlov

1712 silver badges6 bronze badges

On windows, Just go to the ‘Services’. Start/Restart the postgresql-X64 service. It worked for me as my service was in stopped state somehow.

worked for me

answered Sep 10, 2020 at 18:00

devyan91's user avatar

devyan91devyan91

791 silver badge6 bronze badges

This happened because I installed two versions of Postgres (v12 and v13). Psql 12 was installed later so got the port 5433. I needed to use Postgres 12. To fix this particular case:

  • Go to Program Files/Postgres/<required_version>/data

    Open the postgresql.conf file

    Search for Port and change the port number to 5432.

  • Open Windows Services (Press Cmd + R then type services.msc)

  • Stop the service for the version you don’t want (You can stop it permanentally from the Right Click > Properties menu.)

  • Start the service for the version you want.

answered Aug 7, 2021 at 14:30

Nitin Nain's user avatar

Nitin NainNitin Nain

5,0521 gold badge36 silver badges51 bronze badges

1

Summary: There are 2 Solutions:

  1. Open Services and make sure that postgresql-x64-14 is running.

  2. Go to C:Program FilesPostgreSQL14data and open postgresql.conf with notepad, find and change port to e.g 5432 and after that open Services and restart postgresql-x64-14.

answered Apr 11, 2022 at 16:06

Mace's user avatar

MaceMace

1311 silver badge6 bronze badges

1

I think the problem is with your server listening to default public IP address.
For example in the PostgreSQL package, your sever is set to listen to localhost as default public address which when you launch/ run database, the address might be something like ‘127.0.0.1’

To fix you can try change localhost to ‘‘ as in «listen_addresses = ‘‘».

As seen as "listen_addresses = 'localhost'" under «Connection Settings» in the postgresql.conf file.

Also to access your postgresql.conf file, go to:

On Windows, the file is in /Program Files/PostgreSQL/<version>/share/.
On Ubuntu, these files are in /etc/postgresql/<version>/main/.

P.S: Changing the defaults ‘localhost’; to ‘*’ will let your server listen to any public database address either «localhost, 127.0.0.1 etc.

I know you might have fix this, just for others that might run into the same issue in the future. Hope it was helpful

Harsha Biyani's user avatar

answered Jul 31, 2019 at 14:41

Damilare Oyediran's user avatar

When I run psql, it gave me the same error, and it was because I changed the port while setting Postgres in the installation process.

I had to change back to the default port 5432 in PostgreSQL.conf (which can be found in the data directory) i.e

C:Program FilesPostgreSQL14data>

The problem is no more!

answered Jul 19, 2022 at 15:19

James Karino Simel's user avatar

1

goto service and start postgresql-x64-10 service

steps

  • run -> services.msc -> find postgresql-x64-10 -> start the service
  • services image

answered Mar 12, 2021 at 14:33

B G's user avatar

This is a note for a normal user. If using an official installer, it should have a built-in service,

  1. Win+R and type services.msc
  2. Search Postgres service based on the version installed, e.g., «postgresql-x64-13 - PostgreSQL Server 13«
  3. Click stop, start, or restart the service option
  4. If you don’t see start/stop or if these buttons are disabled, then double-click on the PostgreSQL and change startup type to automatic and click on start. This will start the PostgreSQL every time automatically whenever you start your system.

answered Jul 6, 2021 at 2:54

Manoj Swami's user avatar

Manoj SwamiManoj Swami

772 silver badges12 bronze badges

1

I had the same issue, so, I uninstalled postgres. And during reinstallation I noticed the error:
"Failed to load SQL modules into the database cluster"
And:
"Problem running post installation step.Installition may not complete correctly. Error reading file C:/Program Files/PostgreSQL/14/data/postgresql.conf".

I cancelled the installation and then tried again, but with a plain-text password this time, and it worked. It turned out that the special characters in my password were the problem.

ouflak's user avatar

ouflak

2,44810 gold badges44 silver badges49 bronze badges

answered Oct 1, 2021 at 14:48

KanjooM's user avatar

I got this error message when I moved my database to another computer.

I also got some error messages when starting the server first with

pg_ctl -D /wherever/your/database/is start

which were

pg_ctl: another server might be running; trying to start server anyway
server starting

DETAIL: File «/wherever/your/database/is/PG_VERSION» does not contain valid data.

HINT: You might need to initdb.

In my case rather than running initdb this command actually fixed the issue

pg_ctl -D /wherever/your/database/is restart

answered May 10, 2018 at 13:42

uosjead's user avatar

uosjeaduosjead

4166 silver badges5 bronze badges

You might have changed the permissions of the ‘PostgreSQL 12’ in ‘services.msc’. Or maybe it is not started and you are trying to start the server when Postgre 12 is not running.

Try these:

  1. Try to start the ‘PostgreSQL 12’ in ‘services.msc’ manually.
  2. Try restarting your PC
  3. If nothing helps, try reinstalling PostgreSQL (pgAdmin 4) from the scratch.

answered Mar 30, 2021 at 12:18

Nirmal's user avatar

NirmalNirmal

571 silver badge8 bronze badges

Go to C:Program FilesPostgreSQL13data, edit postgresql.conf with notepad.

Change:

#port = 54XX

To:

port = 54XX

(change requires restart)

restart service at «service system» on window.

Tony Joseph's user avatar

Tony Joseph

1,7922 gold badges14 silver badges17 bronze badges

answered Apr 20, 2021 at 17:54

Charlycris's user avatar

Using psql with single quotes fails:

psql -c 'Select version();' 'postgresql://username:password@db.abcdefghi.ap-southeast-2.rds.amazonaws.com:8080/the_db'

psql: could not connect to server: Connection refused
(0x0000274D/10061)
Is the server running on host «localhost» (::1) and accepting
TCP/IP connections on port 5432? could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host «localhost» (127.0.0.1) and accepting
TCP/IP connections on port 5432?

Using double quotes works:

psql -c "Select version();" "postgresql://username:password@db.abcdefghi.ap-southeast-2.rds.amazonaws.com:8080/the_db"

PostgreSQL 10.14 on x86_64-pc-linux-gnu, compiled by x86_64-unknown-linux-gnu-gcc (GCC) 4.9.4, 64-bit
(1 row)

answered Jul 14, 2021 at 1:40

Jeremy Thompson's user avatar

Jeremy ThompsonJeremy Thompson

61.2k33 gold badges186 silver badges318 bronze badges

After a wasting 3-4 hrs i find the solution something like this

First set path in enviroment variable.And then make port and psql connecting same i.e 5432 or 5433

answered Sep 18, 2021 at 3:21

Ashok Mehta's user avatar

If you newly installed the pgAdmin, and you did not remember to install PostgreSQL at that time, you get that error. Make sure you install both pgAdmin And PostgreSQL.

Anurag A S's user avatar

answered Jan 6, 2022 at 11:14

sanket Walunj's user avatar

And so for the new arrivals. If you are using pgAdmin and a Windows operating system, do some research to resolve the issue.

Make sure you have Postgresql installed or active

  • To do this, go to windows services (press Cmd + R and run the services.msc command)

  • Look in the list for a service called postgresql-x{BIT}-{Version}-PostgreSQL Server {Version}

    enter image description here

Solution 1: If you don’t find the service, then PostgreSQL is not installed or has been uninstalled, install it again.

Solution 2: If you find the service, but you still can’t log in, then most likely the service is not active for some reason, select the service and click the Start or Restart button

answered Mar 24, 2022 at 4:41

emrdev's user avatar

emrdevemrdev

2,1153 gold badges9 silver badges15 bronze badges

For me I was getting this error and unable to open the server in PgAdmin is because I very often forgot to start the server, one for the way you can start the server with is by running this commands on cmd, make sure you provide the right path

cd "C:Program FilesPostgreSQL14bin"
pg_ctl -D "C:Program FilesPostgreSQL14data" start

answered Apr 13, 2022 at 6:42

DINA TAKLIT's user avatar

DINA TAKLITDINA TAKLIT

6,7819 gold badges69 silver badges73 bronze badges

If you are a Mac OS user, run these below two commands on termial,

rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql

answered Mar 16 at 10:30

Muhammad Usama Rabani's user avatar

I have installed PostgreSQL. However everytime I try to connect through PGAdmin or through psql it gives me the below error.

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

I checked the postgresql.conf file and the line: listen_addresses = '*' isn’t commented out.

Also this is how my pg_hba.conf file is set:

# IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
  host    all             all             ::1/128                 trust

Can anyone please advise on how I can resolve this issue?

Thanks!

MwamiTovi's user avatar

MwamiTovi

2,39516 silver badges25 bronze badges

asked Mar 14, 2017 at 20:59

dino9239's user avatar

1

Use services «start -> run -> services.msc» and look for the postgresql-[vers] service.

If it is not running try to start it, if it won’t start open the event-viewer (start -> run -> eventvwr) and look for error messages relating to the PostgreSQL service.

answered Oct 31, 2018 at 19:32

tayfun Kılıç's user avatar

tayfun Kılıçtayfun Kılıç

1,93414 silver badges11 bronze badges

1

In my case it was postgres.config. The port was somehow changed to 5433 in there and I do not remember doing it myself lol. So make sure your ports are matching up in your project vs postgres.config

answered Sep 3, 2022 at 20:48

Chris R's user avatar

1

pg_hba.conf.txt has to be called pg_hba.conf.

Beyond this, when you have en authentication error you will get a message similar to:

psql -U nonexistent
psql: FATAL:  Peer authentication failed for user "nonexistent"

The error you are getting means most likely that PostgreSQL is not started on this server.

You can start PostgreSQL with:

service postgresql start
service postgresql status

answered Mar 15, 2017 at 0:40

Gab's user avatar

GabGab

3,3761 gold badge10 silver badges21 bronze badges

1

This worked for me —

In C:Program FilesPostgreSQLdatapostgresql.conf set listen_addresses ='localhost'

Then try —

pg_ctl -D "C:Program FilesPostgreSQL9.5data" start

if already try restarting using pg_ctl

answered Feb 19, 2018 at 11:53

Karthik Sagar's user avatar

It works for me.
Remember, whatever comes after -D should be the path to where you installed PostgreSQL, to the data folder, which holds the pg_hba.conf and postgresql.conf files.

pg_ctl start -D "C:/Program Files/PostgreSQL/9.6/data"

answered Mar 17, 2020 at 5:41

Kairat Koibagarov's user avatar

Maybe you can try this.
Open cmd and insert.

"C:Program FilesPostgreSQL11binpg_ctl.exe" runservice -N "postgresql-x64-11" -D "C:Program FilesPostgreSQL11data" -w

Hit enter…

Dharman's user avatar

Dharman

30.4k22 gold badges84 silver badges133 bronze badges

answered May 24, 2019 at 12:15

Yan LimaBenua's user avatar

An incorrect entry in the configuration file or firewall restriction generally triggers PostgreSQL connection refused error.

As a part of our PostgreSQL Management Services, we help webmasters and web hosts resolve PostgreSQL such as this every day.

postgresql connection refused

Today let’s discuss the top reasons for connection refused error in PostgreSQL and how to resolve the error.

What triggers PostgreSQL connection refused error?

PostgreSQL is a highly extensible and handy database management system. And thus it is one of the popular database systems.

But many times, when users try to connect to psql it ends up in connection errors. psql is the interactive terminal-based program, that enables PostgreSQL queries.

A typical error message looks like

Error: psql: could not connect to server: Connection refused Is the server running on host “xxx.xxx.xxx.xxx” and accepting TCP/IP connections on port 5432?

When the PostgreSQL’s server daemon is not running in the server, it can trigger connection refused error. At the same time, several other reasons can also trigger this error.

Let’s discuss a few reasons why the psql service fails. We will also discuss how our Support Engineers fix this error.

PostgreSQL service status

First parameter to cross-check during a psql connection refused error is the status of the PostgreSQL service.

We can confirm this by cross-checking if a Postgres process in running in the server. In a Linux like environment, we can use

ps -ef | grep postgres
service postgresql status

If the service is not running in the server, then we restart it using the command.

service postgresql restart

At times, this failure could be a result of a corrupted Postgresql service. A reinstall of the service with the apt-get install or yum install would be required to fix the issue.

Enable TCP/IP connections

If the error is not related to the failed status of the service, it is more likely to be related to TCP/IP connections disabled in the Postgresql configuration file.

To correct this, we need to edit posgresql.conf file. The file location in Ubuntu and Centos is :

In Ubuntu: vi /etc/postgresql/9.5/main/posgresql.conf

in CentOS 7: vi /usr/pgsql-10/share/postgresql.conf

The listen_address parameter in the configuration file determines the restrictions on TCP/IP connections. To allow TCP/IP connections, from all IP addresses it should be set to 0.0.0.0.  Alternatively, to allow connections to the specific IP address of the server, listen_address parameter need to be edited with only those IP addresses.

If this configuration is left blank or set to localhost, PostgreSQL will not allow external TCP/IP connections.

Finally, we restart the service to save the changes.

Allow remote host

If the users are not able to connect to psql from a remote IP, then also we get the same error.

In this case, the Support Team edits the postgresql.conf file and allow the IP from which the customer is trying to access.

Finally, we restart the service to save the changes.

Firewall restrictions

At times, the restrictions placed by the server firewall can also trigger connection refused error.

If the psql port 5432 is not open for connection or if there is any restriction on the IP address from which the TCP/IP connection occurs, it may trigger connection refused error.

Our Support engineers cross-check the firewall for any restrictions and then remove them to fix the issue.

[Still, having trouble connecting PostgreSQL? – We will fix it for you.]

Conclusion

In short, several reasons can trigger the PostgreSQL connection refused error. This includes corrupted Postgresql service, firewall restrictions, incorrect entries in the configuration file etc. We saw the top 4 reasons for this error and how our Support Engineers fix them.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

To avoid “Connection refused”, Configuration is required for client to access PostgreSQL database server.

When trying to connect to a new PostgreSQL database, the following error occurred:

C:Program Files (x86)pgAdmin 4v1runtime>psql -h 192.168.78.51 -d postgres
psql: could not connect to server: Connection refused (0x0000274D/10061)
 Is the server running on host "192.168.78.51" and accepting
 TCP/IP connections on port 5432?
Check postgresql server is up and running
$ systemctl status postgresql-9.6

● postgresql-9.6.service - PostgreSQL 9.6 database server
 Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
 Active: active (running) since Sun 2017-07-16 18:08:47 AEST; 15min ago
 Process: 1238 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 1243 (postmaster)
 CGroup: /system.slice/postgresql-9.6.service
 ├─1243 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
 ├─1246 postgres: logger process
 ├─1248 postgres: checkpointer process
 ├─1249 postgres: writer process
 ├─1250 postgres: wal writer process
 ├─1251 postgres: autovacuum launcher process
 ├─1252 postgres: stats collector process
 └─1822 postgres: postgres postgres 192.168.78.1(56572) idle
From client telnet to server
C:Userspostgres>telnet 192.168.78.51 5432
Connecting To 192.168.78.51...Could not open connection to the host, on port 5432: Connect failed
From server check listener port 5432 not opened on IP 192.168.78.1
$ netstat -ntl|grep 5432
tcp  0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
tcp6 0 0       ::1:5432 :::*      LISTEN
Allow remote IP address to access PostgreSQL
$ su - postgres
$ vi /var/lib/pgsql/9.6/data/pg_hba.conf

...
..
.

# IPv4 local connections:
host all all 127.0.0.1/32 ident
host all all 192.168.78.51/24 md5
...
..
.
Edit /var/lib/pgsql/9.6/data/postgresql.conf

# – Connection Settings –

...
..
.
listen_addresses = '192.168.78.51,localhost' # what IP address(es) to listen on;
 # comma-separated list of addresses;
 # defaults to 'localhost'; use '*' for all
 # (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
...
..
.
Stop and Start PostgreSQL server
$systemctl status postgresql-9.6

$systemctl stop postgresql-9.6

$systemctl start postgresql-9.6
Connect to PostgreSQL database successfully
C:Program Files (x86)pgAdmin 4v1runtime>psql -h 192.168.78.51 -d postgres -U postgres
Password for user postgres:
psql (9.6.3)
WARNING: Console code page (850) differs from Windows code page (1252)
 8-bit characters might not work correctly. See psql reference
 page "Notes for Windows users" for details.
Type "help" for help.

postgres=# l
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
(3 rows)

In this document I am going to discuss about some of the most frequent errors faced by users while connecting to PostgreSQL. This is document is to help the beginners who started working on PostgreSQL. PostgreSQL has a process called postmaster which acts as listener process, by default the process listens on 5432.

There are different tools available to connect to PostgreSQL, here I am going to use the psql command line tool but there is no difference in error messages.

ERROR – 1

[root@localhost ~]# psql
psql: error: FATAL: role “root” does not exist

By default “psql” command line tool use the operating system user as the database user and localhost the hostname if the user name is not passed as argument, here I logged in as the root user and tried to login to PostgreSQL without username which caused the error.

Solution

give the username when logging into the database, by default the psql command line take the username as the database name, so no need to pass the database as I am trying to connect to PostgreSQL.

psql -U postgres -h localhost

ERROR – 2

[root@localhost ~]# psql -U postgres
psql: error: FATAL: Peer authentication failed for user “postgres”

Solution

By default PostgreSQL installation using yum repository configure the pg_hba.conf with peer, ident authentication. So change the peer and ident authentication methods to md5 and reload the configuration.

[root@localhost data]# psql -U postgres
Password for user postgres: 
psql (12.5)
Type "help" for help.

postgres=# 

ERROR – 3

[root@localhost data]# psql -h 192.168.159.151 -U postgres -d postgres
psql: error: could not connect to server: Connection refused
Is the server running on host “192.168.159.151” and accepting
TCP/IP connections on port 5432?

This is the common error users get when they connect to PostgreSQL. The error represents there is no listener for host and port combination.

Solution

Most common issue is listener_address parameter is set to ‘localhost’ or port set to a non default value.

postgres=# show listen_addresses ;
 listen_addresses 
------------------
 localhost
(1 row)

postgres=# show port;
 port 
------
 5432
(1 row)

Here the issue is listener_address is localhost, so changed it to ‘*’ where it listens on all ip’s in my host. I have only one ip configured, if your server has multiple ip’s then set the ip address on which the PostgreSQL connections should happen.

Configure the listener_address or change the port and restart the PostgreSQL cluster.

postgres=# alter system set listen_addresses to '*';
ALTER SYSTEM
postgres=# q
-bash-4.2$ /usr/pgsql-12/bin/pg_ctl -D /var/lib/pgsql/project/data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2020-12-20 07:12:20.317 PST [3051] LOG:  starting PostgreSQL 12.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-12-20 07:12:20.318 PST [3051] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-12-20 07:12:20.318 PST [3051] LOG:  listening on IPv6 address "::", port 5432
2020-12-20 07:12:20.319 PST [3051] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-12-20 07:12:20.320 PST [3051] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-12-20 07:12:20.332 PST [3051] LOG:  redirecting log output to logging collector process
2020-12-20 07:12:20.332 PST [3051] HINT:  Future log output will appear in directory "log".
 done
server started

ERROR – 4

-bash-4.2$ psql -h 192.168.159.151 -U postgres -d postgres
psql: error: FATAL: no pg_hba.conf entry for host “192.168.159.151”, user “postgres”, database “postgres”, SSL off

This is another common error which users will face due to missing entry for the user host address (ip address) in pg_hba.conf file. host based authentication file presents in data directory which has a list of ip addresses or host addresses allowed to connect to PostgreSQL.

Solution

Make an entry like below for the user host or subnet in pg_hba.conf file. Any ip address with 192.168.X.X will be able to connect to the host.

host	all		all		192.168.0.0/16		md5

ERROR – 5

-bash-4.2$ psql -h 192.168.159.151 -U postgres -d postgres
psql: error: FATAL: sorry, too many clients already

-bash-4.2$ psql -h 192.168.159.151 -U viswamitra -d postgres
psql: error: FATAL: remaining connection slots are reserved for non-replication superuser connections

-bash-4.2$ psql -U viswamitra -d postgres -h 192.168.159.151
psql: error: FATAL: too many connections for role “viswamitra”

This is also a common error users face after started using the database in application. This errors will come after the connection limit is reached to the maximum configured value.

Connection limit can be set at different levels

  1. cluster – which is applicable for all users and databases
  2. user – which is applicable for the user
  3. database – which is applicable for the given database

Solution

Login to the database as a super user, if there are any idle connections from longer time close them and check the application connection pool settings to close the idle connections after some time interval.

postgres=# select pid,datname,usename,application_name,state,now()-state_change as idle_duration from pg_stat_activity where state = 'idle';
 pid  | datname  |  usename   | application_name | state |  idle_duration  
------+----------+------------+------------------+-------+-----------------
 3656 | alex     | postgres   | psql             | idle  | 00:14:06.647055
 3652 | alex     | postgres   | psql             | idle  | 00:14:11.718486
 3623 | postgres | viswamitra | psql             | idle  | 00:15:47.530912
(3 rows)

postgres=# select pg_terminate_backend(3656);
 pg_terminate_backend 
----------------------
 t
(1 row)

If there are no idle sessions to cancel and the error is first and second one in the list increase the “max_connections” in postgresql.conf file. This requires a reboot of the PostgreSQL cluster to make it effect.

postgres=# alter system set max_connections to '1000';
ALTER SYSTEM
postgres=# 

/usr/pgsql-12/bin/pg_ctl -D /var/lib/pgsql/project/data restart

For third error (5.3) , the connection limit is configured at the user level, it can be changed using the alter user statement.

postgres=# alter user viswamitra connection limit 100;
ALTER ROLE

Понравилась статья? Поделить с друзьями:
  • Psql ошибка важно роль user не существует
  • Psql ошибка важно роль root не существует
  • Psp что делать при ошибке 80020148
  • Psp ошибка подключения невозможно подключиться к точке доступа
  • Psp ошибка 80020148 что делать