Cant connect to PostgreSQL database - postgresql

I have problem connecting to database. I recive message that role "Darko" does not exists. This is the sequence of my commands:
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data -l logfile
Server starting /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data The files belonging to this database system will be owned by user "darko".
This user must also own the server process.
The database cluster will be initialized with locales COLLATE: en_US.UTF-8 TYPE: en_US.UTF-8 MESSAGES: en_US.UTF-8 MONETARY: hr_HR.UTF-8 NUMERIC: hr_HR.UTF-8 TIME: en_US.UTF-8
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. initdb: directory "/usr/local/pgsql/data" exists but is not empty
If you want to create a new database system, either remove or empty the directory "/usr/local/pgsql/data" or run initdb with an argument other than "/usr/local/pgsql/data". /usr/local/pgsql/bin/psql psql:
FATAL: role "darko" does not exist

You're trying to initialize Postgres cluster (main directory where all your Postgres data and logs will be stored) in "/usr/local/pgsql/data" directory.
Create another directory, anywhere and pass full path to it to -D option of initdb.
It will initialize your Postgres cluster.
During this process, a default database role will be created. If you run initdb under your regular OS user ("darko") -- it will create database user with the same name. But usually people run posgres tools (including initdb) under a separate OS user, "postgres" -- and if you will do it (like "sudo -u postgres initdb -D ..."), initdb will create database role "postgres" instead. (BTW, in Postgres terminology, database user and database role are the same things).
Then, once initbd has successfully created your Postgres cluster, you can check it using "ls" -- directory must contain subdirectories such as "base", "global", "pg_xlog", config files, etc.
Then you need to run Postgres, it's done using pg_ctl command, and you need to pass path to your cluster, again with -D option:
pg_ctl -D /path/to/cluster
If it runs successfully, open another terminal tab/window and try to connect using psql and corresponding database role (either "darko" or "postgres", based on your previous decision):
psql -U darko template1
"template1" is a database name which is always present, it serves as a template for all future databases you'll create.
Also, before you try to start Postgres with pg_ctl, it's worth to check if you have already some Postgres running:
ps ax | grep postgres
-- if you have another Postgres running, you can shut it down using "pg_ctl stop" with corresponding -D option pointing to the proper cluster directory.
Example (Ubuntu):
$ ps ax | grep postgres | grep D
21996 ? S 23:37 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf
To stop it gracefully, just run:
/usr/lib/postgresql/9.6/bin/pg_ctl -D /var/lib/postgresql/9.6/main stop

initdb is a tool used to create a cluster (see PostgreSQL documentation for cluster definition in this context). It seems your allready did that as your directory isn't empty. Don't do it anymore.
Then, you're trying to connect with psql. By default, it's trying a socket connection with the same name for database user as your OS user. As you didn't create a role named 'darko', it fails.
What you have to do is trying to connect as postgres user. Try this to connect:
sudo -u postgres psql
You should be abble to connect with that line (assuming you didn't change the pg_hba.conf file) and then you should be abble to create your users and databases.
Here's the documentation page for psql.

Related

initdb in Postgres.app does not create postgres &template1 database

I installed postgres.app 2.4.2 on macos(big sur 11.4).
I wanted to initdb with locale=C, so run the command initdb -D "/Users/xxx/Library/Application Support/Postgres/var-131" -U postgres --encoding=UTF-8 --locale=C --auth-local=trust and the logging
(base) > $ initdb -D "/Users/xxx/Library/Application Support/Postgres/var-13_1" -U postgres --encoding=UTF-8 --locale=C --auth-local=trust
The files belonging to this database system will be owned by user "yy".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /Users/xxx/Library/Application Support/Postgres/var-13_1 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Hongkong
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... 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.
Success. You can now start the database server using:
pg_ctl -D '/Users/xxx/Library/Application Support/Postgres/var-13_1' -l logfile start
In PG document(https://www.postgresql.org/docs/13/app-initdb.html), it said:
generating the shared catalog tables (tables that belong to
the whole cluster rather than to any particular database),
and creating the template1 and postgres databases
It was wired that there was no postgres and template1 database by default.
I tried to login using psql and it displayed role "username" does not exist.
I also tried createuser -U postgres -s $USER and it showed the error
(base) > $ createuser -U postgres -s $USER
createuser: error: could not connect to database template1: FATAL: Peer authentication failed for user "postgres"
I was very confused, and how can I got to solved the question or should I switch to homebrew version?

"Permission denied" when trying to initialize PostgreSQL database server on Linux

I am trying to set up PostgreSQL in Linux, but the following error comes up:
[postgres#kibearch ~]$ initdb --pgdata=/var/lib/pgsql/data
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.UTF-8".
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.
creating directory /var/lib/pgsql/data ... initdb: error: could not create directory "/var/lib/pgsql": Permission denied
If I try to use sudo initdb --pgdata=/var/lib/psql/data/, it says that initdb can not be used with sudo.
Here are the steps I did:
sudo pacman -S postgresql
Switched to user postgres user using this command: su - postgres
Tried to initialize the server but the aforementioned error showed up
What can I do?
As root:
mkdir /var/lib/pgsql
chown postgres /var/lib/pgsql
As postgres:
initdb --pgdata=/var/lib/pgsql/data

Creating a database and connection with client for PostgreSQL on OSX

How can I start a PostgreSQL database and connect with a client in terminal?
I've tried this after installing postgreSQL with brew.
which psql
Results in this:
/usr/local/bin/psql
Then I try to start database with:
pg_ctl init -D /usr/local/bin/psql
Which gives:
The files belonging to this database system will be owned by user "matt".
This user must also own the server process.
The database cluster will be initialized with locale "en_AU.UTF-8".
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.
initdb: could not access directory "/usr/local/bin/psql": Not a directory
pg_ctl: database system initialization failed
You must create a data directory first and input its name after -D. Say if the data directory you created is /usr/local/bin/psql, then your init command will be
pg_ctl init -D /usr/local/bin/psql/data
From the PostgreSQL pg_ctl documentation:
-D datadir
--pgdata datadir
Specifies the file system location of the database configuration files. If this is omitted, the environment variable PGDATA is used.
You are specifying your pgsql executable as your data directory.

starting postgresql and pgadmin in windows without installation

How can I start PostgreSQL and pgAdmin III in windows without installation. I do not have admin rights in a system. so I need to start the application without installing . How can I do that?
Download the ZIP file from https://www.enterprisedb.com/download-postgresql-binaries
Unzip the archive into a directory of your choice (the archive is created such that unzipping it, it will create a directory pgsql with everything else below that)
Run initdb (this can be found in the subdirectory pgsql\bin)
initdb -D c:\Users\Arthur\pgdata -U postgres -W -E UTF8 -A scram-sha-256
This will create the postgres "data directory" (aka the "cluster") in c:\Users\Arthur\pgdata. You need to make sure that the user running this command has full read/write privileges on that directory.
-U postgres creates the superuser as postgres, -W will prompt you for the password of the superuser, -E UTF8 will create the database with UTF-8 encoding and -A scram-sha-256 enables the password authentication.
To start Postgres, run:
pg_ctl -D c:\Users\Arthur\pgdata -l logfile start
this has(!) to be done as the user who ran initdb to avoid any problems with the access to the data directory.
To shutdown Postgres, run:
pg_ctl -D c:\Users\Arthur\pgdata stop
psql.exe (the command line client) is located in the bin directory. Starting with Postgres 9.6 the pgAdmin executable pgAdmin4.exe is located in the sub-directory "pgAdmin 4\bin".
Optionally create a Windows service to automatically run Postgres (must be run using a Windows administrator account)
pg_ctl register -N postgresql -D c:\Users\Arthur\pgdata
Thank you. This worked for me. But, i was getting error while starting psql.exe
The error was "psql: FATAL : role [user] does not exist."
To resolve this, i followed the below steps:
Be on the same folder path (where you have initdb.exe) i.e. source-folder/pgsql/bin
Run "psql -U postgres". This will ask for password.
Now enter the password that was set during postgres intialization. This will open the psql command prompt.
Hope this helps.. :)
I tried above method its working fine, but when i tried to connect it via JDBC i used to get
"The authentication type 10 is not supported. Check that you have configured the pg_hba.conf file to include the client's IP address or
subnet, and that it is using an authentication scheme supported by the
driver."
This happens because scram-sha-256 has some issue(refer here), i couldn't understand in depth, so i changed it to md5, All started to work smoothly.hope it helps
.\initdb -D D:\database\dbdata -U postgres -W -E UTF8 -A md5

Fail to restore a postgres db

i had no problem to restore a postgres database but today when i tried to restore it as before it gave me a error, here whats i have done:
i tried to restore a postgres database as follows:
mostafa#jamareh:~/.../DB$ sudo -u postgres psql -d dbname -f dbname.sql
but it gives me this:
could not change directory to "/home/....../DB"
dbname.sql: No such file or directory
also i tried sudo -su postgres... but it doesn't work neither
what can i do to resolve it ?
thanks in advance,
From an initial look, it could be that the postgres UNIX user does not have access to your dbname.sql file because of permission issues.
You could either change the permissions (or move the file to a place that is readable by UNIX user postgres, say, /tmp), or, better, restore the database from your own UNIX user, but with the proper database user. For example, you would have:
mostafa#jamareh:~/.../DB$ psql -d dbname -U postgres -f dbname.sql
Note that there is no sudo, but that you tell psql to run the script as DB user postgres (assuming that you have the credentials for the postgres DB user).