Unable to build the PostgreSQL environment. FATAL: database "t" does not exist - postgresql

I installed postgres from brew.
I got an error, I tried to delete the postmaster.id
The file seems to be missing.
rm -f data/postmaster.pid
It had no effect.
I tryed
$brew install postgres
$psql --version
psql (PostgreSQL) 14.1
$postgres --version
postgres (PostgreSQL) 14.1
$psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "t" does not exist
$postgres -D /usr/local/var/postgres
2022-01-28 14:27:14.406 JST [94892] LOG: starting PostgreSQL 14.1 on aarch64-apple-darwin21.1.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.3), 64-bit
2022-01-28 14:27:14.408 JST [94892] LOG: could not bind IPv6 address "::1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] WARNING: could not create listen socket for "localhost"
2022-01-28 14:27:14.408 JST [94892] FATAL: could not create any TCP/IP sockets
2022-01-28 14:27:14.409 JST [94892] LOG: database system is shut down
$rm /usr/local/var/postgres/postmaster.pid
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
$sudo lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 94724 t 7u IPv6 0xe42d991dab624b7 0t0 TCP localhost:postgresql (LISTEN)
postgres 94724 t 8u IPv4 0xe42d991d7856c0f 0t0 TCP localhost:postgresql (LISTEN)

I don't know why you insist in wrecking your PostgreSQL server by removing the PID file. I wish you would stop doing that.
The error message means that you are trying to connect to database t, but that database does not exist. Since you do not explicitly specify a database, the causes could be:
you are operating system user t, so the connection defaults to database t
the environment variable PGDATABASE is set to t
the environment variable PGUSER is set to t, and the connection defaults to the same database name as the user name
In any event, the solution is to pick an existing database and use
psql -d dbname

Related

Unable to setup Postgres in Nix-Shell

I'm working on a Phoenix/Elixir project which is managed using nix-shell. I'm having issues regarding postgres setup.
I'm starting server using pg_ctl -D $PGDATA start and it outputs as:
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2021-09-01 19:19:00.780 PKT [72367] LOG: listening on IPv4 address "127.0.0.1", port 5432
2021-09-01 19:19:00.784 PKT [72367] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2021-09-01 19:19:00.806 PKT [72369] LOG: database system was interrupted; last known up at 2021-09-01 19:14:57 PKT
2021-09-01 19:19:00.848 PKT [72369] LOG: database system was not properly shut down; automatic recovery in progress
2021-09-01 19:19:00.849 PKT [72369] LOG: redo starts at 0/16D84A0
2021-09-01 19:19:00.849 PKT [72369] LOG: invalid record length at 0/16D8580: wanted 24, got 0
2021-09-01 19:19:00.849 PKT [72369] LOG: redo done at 0/16D8548
2021-09-01 19:19:00.862 PKT [72367] LOG: database system is ready to accept connections
done
server started
when I execute createuser postgres --createdb --echo in nix-shell envoirnment it produces following error even though the run directory exists.
createuser: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/run/postgresql/.s.PGSQL.5432"?
No other postgres instance is running.
In pg_hba.conf auth method is set to trust.
unix_socket_directories is set to /tmp.
shell.nix looks something like
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
inherit (lib) optional optionals;
elixir = beam.packages.erlangR22.elixir_1_10;
postgresql = postgresql_11;
in
mkShell {
buildInputs = [
ps
elixir
coreutils
which
git
postgresql
redis
doxygen
mongodb-tools
redis-dump
(python37.withPackages(ps: with ps; [ credstash awscli ]))
cmake
nix-prefetch-git
zlib
jq
teleport
]);
# Fix GLIBC Locale
LOCALE_ARCHIVE = lib.optionalString stdenv.isLinux
"${pkgs.glibcLocales}/lib/locale/locale-archive";
LANG = "en_US.UTF-8";
# Put the PostgreSQL and Redis databases in the project diretory.
PGDATA="./.db/postgres";
RDDATA="./.db/redis";
shellHook = ''
ERL_INCLUDE_PATH="${erlangR22}/lib/erlang/usr/include";
}
I totally a noob to Nix and have no idea how to get it working.
Any help would be appreciated, thanks.
You have to tell createuser to connect via the socket in /tmp:
createuser -h /tmp -U postgres --createdb --echo newuser
You've set a custom path in the postgres config, so you probably also need to specify that with pqsl / createuser. In other words createuser -h /tmp/ ....

Postgres: "Failed to load sql modules into the database cluster" and other errors

I run Windows and I am trying to install postgres, but I can't get it to work. During installation, I get the error:
Failed to load sql modules into the database cluster
When I try to access the server in pgadmin or via SQL Shell, I get the 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 have found a log in the data-folder saying:
2021-04-08 14:53:44.315 CEST [11088] LOG: starting PostgreSQL 13.2, compiled by Visual C++ build 1914, 64-bit
2021-04-08 14:53:44.321 CEST [11088] LOG: could not listen on IPv6 address "::": Permission denied
2021-04-08 14:53:44.326 CEST [11088] LOG: could not listen on IPv4 address "0.0.0.0": Permission denied
2021-04-08 14:53:44.327 CEST [11088] WARNING: could not create listen socket for "*"
2021-04-08 14:53:44.328 CEST [11088] FATAL: could not create any TCP/IP sockets
2021-04-08 14:53:44.331 CEST [11088] LOG: database system is shut down
Can someone please help me?
I had the same issue and followed different tips and tricks available on the internet but nothing worked.
I fixed that issue by following these steps:
First disable antivirus for an hour
Create a folder in either C drive or D drive and give full permission to User
Then again install a fresh application. If you have already installed then uninstall that and install from starting.
Make sure to choose that folder only at the time of installation

I can't connect to my postgres database on Docker from Intellij

I'm doing the same steps as described in: https://medium.com/better-programming/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7
but when I try to connect to my postgres (with password mysecretpassword) from Intellij I get the following error:
The specified database user/password combination is rejected: [28P01] FATAL: password authentication failed for user "postgres"
Of course I can connect to my db from cmd command:
$ psql -h localhost -p 5432 -U postgres -W Password for user postgres:
psql (9.5.5, server 10.3 (Debian 10.3-1.pgdg90+1)) WARNING: psql major version 9.5, server major version 10. Some psql features might not work.
Type "help" for help. postgres=# \l
My container is up:
What is going on? I don't have any idea... I use postgres driver 42.2.5 in Intellij
Logs from container:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
syncing data to disk ... ok
Success. You can now start the database server using:
pg_ctl -D /var/lib/postgresql/data -l logfile start
waiting for server to start....2020-09-30 11:17:42.613 UTC [45] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.618 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.642 UTC [46] LOG: database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.649 UTC [45] LOG: database system is ready to accept connections
done
server started
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2020-09-30 11:17:42.700 UTC [45] LOG: received fast shutdown request
waiting for server to shut down....2020-09-30 11:17:42.705 UTC [45] LOG: aborting any active transactions
2020-09-30 11:17:42.708 UTC [45] LOG: background worker "logical replication launcher" (PID 52) exited with exit code 1
2020-09-30 11:17:42.708 UTC [47] LOG: shutting down
2020-09-30 11:17:42.737 UTC [45] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2020-09-30 11:17:42.836 UTC [1] LOG: starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.836 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-09-30 11:17:42.836 UTC [1] LOG: listening on IPv6 address "::", port 5432
2020-09-30 11:17:42.845 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.881 UTC [54] LOG: database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.888 UTC [1] LOG: database system is ready to accept connections
create dir for volume: C:\Users{user}\docker\postgres_data\data
Connect to Postgres on Windows:
docker run -p 5432:5432 --name postgres -v C:\Users\{user}\docker\postgres_data\data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=root -d postgres:11.3
Connect to Postgres from CMD and create DB (only after first creation container on empty volume):
docker ps
get the output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e626f4c7b958 postgres:11.3 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:5432->5432/tcp postgres
Run:
docker exec -it {CONTAINER ID} bash
psql -h localhost -U postgres
psql -U postgres
Connect from Intellij with PASSWORD=root:
It seems that you have running postgresql instance on 5432 port. Check what is running on port 5432:
sudo lsof -i :5432
If there are any records, you could manually kill the processes that are using this port (man kill) and re-run docker container. In my case there was another local instance of postgresql running on this port, so the following helps me:
sudo pkill -u postgres
Or (more preferable) just specify a different port mapping for your docker postgresql (for e.g. 5433):
docker run -p 5433:5432 ...other_your_flags... postgres:latest
try to remove your PostgreSQL the one you have installed locally, if it exists. Because my problem was precisely that the idea did not understand where to connect, to the docker survey or to that installed database on my computer

pg_Ctl -data directory has wrong ownership

I am unable to start the Postgres server and whenever I use pg_ctl I am getting the following error - can some one help me to fix this. I changed the folder permissions using CHmod and tried running with Sudo -s also but still the problem exists.
one error I did was, I deleted the Postmaster.pid when the server was running- post this I am getting this issue when ever I try to start the server through pg_ctl and another error when I use the pgadmin.
Any suggestions here will be really helpful- thanks.
Using Macos Shell command :
'pg_ctl start -D /Library/PostgreSQL/12/data waiting for server to start....2020-05-05 11:40:04.838 IST [1216] FATAL: data directory "/Library/PostgreSQL/12/data" has wrong ownership 2020-05-05 11:40:04.838 IST [1216] HINT: The server must be started by the user that owns the data directory. stopped waiting pg_ctl: could not start server Examine the log output.'
Using pgadmin the error is as follows :
'could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5434? 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 5434?'
p.s. : I modified the hba.conf and also the postgres.conf files to allow connection from the local ip
Error received on 5May
waiting for server to start....2020-05-05 19:54:13.029 IST [7274] LOG: starting PostgreSQL 12.2 on x86_64-apple-darwin, compiled by Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn), 64-bit
2020-05-05 19:54:13.030 IST [7274] LOG: listening on IPv6 address "::", port 5433
2020-05-05 19:54:13.030 IST [7274] LOG: listening on IPv4 address "0.0.0.0", port 5433
2020-05-05 19:54:13.030 IST [7274] LOG: listening on Unix socket "/tmp/.s.PGSQL.5433"
2020-05-05 19:54:13.039 IST [7274] LOG: redirecting log output to logging collector process... 2020-05-05 19:54:13.039 IST [7274] HINT: Future log output will appear in directory "log" stopped waiting .. pg_ctl: could not start server
Examine the log output.
Log file details
2020-05-05 21:29:30.748 IST [8853] LOG: invalid authentication method "0.0.0.0/0"
2020-05-05 21:29:30.748 IST [8853] CONTEXT: line 80 of configuration file "/Library/PostgreSQL/12/data/pg_hba.conf"
2020-05-05 21:29:30.748 IST [8853] FATAL: could not load pg_hba.conf
2020-05-05 21:29:30.749 IST [8853] LOG: database system is shut down
Details of my pg_HBA conf
# "local" is for Unix domain socket connections only
local all all 0.0.0.0/0 md5
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all md5
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all 0.0.0.0/0 md5
host all all ::/0 md5
latest log file
bash-3.2$ cat postgresql-2020-05-05_221328.log
2020-05-05 22:13:28.794 IST [9834] LOG: database system was interrupted; last known up at 2020-05-05 22:13:09 IST
2020-05-05 22:13:28.872 IST [9834] LOG: database system was not properly shut down; automatic recovery in progress
2020-05-05 22:13:28.874 IST [9834] LOG: redo starts at 0/17742C8
2020-05-05 22:13:28.874 IST [9834] LOG: invalid record length at 0/1774300: wanted 24, got 0
2020-05-05 22:13:28.874 IST [9834] LOG: redo done at 0/17742C8
2020-05-05 22:13:28.881 IST [9832] LOG: database system is ready to accept connections
......
also I found this error while staring the server and the PID is chaning everytime..
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-05-05 22:09:21.941 IST [9746] FATAL: lock file "postmaster.pid" already exists
2020-05-05 22:09:21.941 IST [9746] HINT: Is another postmaster (PID 9735) running in data directory "/Library/PostgreSQL/12/data"?
stopped waiting
pg_ctl: could not start server
Examine the log output.
bash-3.2$ kill -9 9735
bash-3.2$ pg_ctl start -D /Library/PostgreSQL/12/data
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2020-05-05 22:09:35.829 IST [9758] FATAL: lock file "postmaster.pid" already exists
2020-05-05 22:09:35.829 IST [9758] HINT: Is another postmaster (PID 9747) running in data directory "/Library/PostgreSQL/12/data"?
stopped waiting
pg_ctl: could not start server
Examine the log output.
502 9833 9832 0 10:13PM ?? 0:00.00 postgres: logger
502 9835 9832 0 10:13PM ?? 0:00.00 postgres: checkpointer
502 9836 9832 0 10:13PM ?? 0:00.04 postgres: background writer
502 9837 9832 0 10:13PM ?? 0:00.01 postgres: walwriter
502 9838 9832 0 10:13PM ?? 0:00.01 postgres: autovacuum launcher
502 9839 9832 0 10:13PM ?? 0:00.01 postgres: stats collector
502 9840 9832 0 10:13PM ?? 0:00.00 postgres: logical replication launcher
0 9641 9504 0 10:03PM ttys000 0:00.02 sudo -u postgres -s /bin/bash
502 9904 9642 0 10:37PM ttys000 0:00.00 grep postgres
The data directory should be owned by the postgres user and have user-only access (700 or u+rwx)
Does this match what you have set up?
Thom Brown
Disclosure: I am an EnterpriseDB employee.
Try running this code
pg_ctl -D /usr/local/var/postgres start

could not bind IPv4 socket: Permission denied

I am trying to set up a new instance of PostgreSQL 9.6 on a machine. I have tested it on another machine and its working fine on that machine. But the same process is not working on new machine. Below are the steps I am using
created a new data directory with below command
/opt/rh/rh-postgresql96/root/bin/initdb -D /var/lib/pgsql/9.6/data/
created a service file /etc/systemd/system/rh-postgresql96-inst2.service with below content
.include /lib/systemd/system/rh-postgresql96-postgresql.service
[Service]
Environment=PGDATA=/var/lib/pgsql/9.6/data/
Environment=PGPORT=5433
User=postgres
Group=root
registered service using command systemctl enable rh-postgresql96-inst2
now using command systemctl start rh-postgresql96-inst2 to start service.
All these steps are working fine on one machine but not on the 2nd one.
I am getting below error while starting service on the 2nd machine
rh-postgresql96-inst2.service - PostgreSQL database server
Loaded: loaded (/etc/systemd/system/rh-postgresql96-inst2.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2018-06-18 09:59:01 UTC; 10s ago
Process: 7552 ExecStart=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT} (code=exited, status=1/FAILURE)
Process: 7550 ExecStartPre=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-check-db-dir %N (code=exited, status=0/SUCCESS)
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Permission denied
HINT: Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
LOG: database system is shut down
systemd[1]: rh-postgresql96-inst2.service: control process exited, code=exited status=1
systemd[1]: Failed to start PostgreSQL database server.
systemd[1]: Unit rh-postgresql96-inst2.service entered failed state.
systemd[1]: rh-postgresql96-inst2.service failed.
However, I am able to start service using pg_ctl.
Also, I have checked with netstat, lsof command to check if any other postgresql instance is running on port 5433 but its not the case.
Infact i tried 5431, 5434 ports also but server is not starting up
Instead of turning of SELinux you should allow postgres to bind to port 5433 in SELinux.
There is a port parameter postgresql_port_t which by default has port 5432 and 9898.
semanage port -l | grep post
postgresql_port_t tcp 5433, 9898
What you could do is simply add port 5433 to this list.
semanage port -a -t postgresql_port_t 5433 -p tcp
semanage port -l | grep post
postgresql_port_t tcp 5433, 5432, 9898
After that you can start your postgres server listening on port 5433
systemctl enable rh-postgresql96-postgresql
systemctl start rh-postgresql96-postgresql
netstat -tulpn
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 2847/postgres
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 2775/postgres
There is also a handy tool called audit2allow to help debug selinux problems.
audit2allow -m whatiswrong < /var/log/audit/audit.log > /root/showme.te
The file showme.te show you why SELinux is not allowing the service to do what you need.
You should not turn off SELinux just because it's hard to understand or if you don't know how it works. Instead you should study it :)
I reccomend this lecture from the Red Hat Summit https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals
This issue was related to SELinux.
When I run command sestatus on both machines, output was a little bit different.
One server had Current mode: permissive and 2nd one had Current mode: enforcing.
So I changed the current mode to permissive on the 2nd machine using command setenforce 0.
and it resolved the permission related issue. Now I am able to start 2nd instance.