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

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

Related

Installation issue Apache Age and Postgress on ubuntu

I am trying to install Apache age and Postgres from source code but i am not able to start postgres Server
`
The files belonging to this database system will be owned by user "faruukh".
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.
initdb: could not access directory "/usr/local/pgsql/bin/psql": Not a directory
faruukh#ubuntu:~$ sudo chown -R farrukh /usr/local/pgsql/
chown: invalid user: ‘farrukh’
faruukh#ubuntu:~$ sudo chown -R faruukh /usr/local/pgsql/
faruukh#ubuntu:~$ which psql
/usr/local/pgsql/bin//psql
faruukh#ubuntu:~$ ls /usr/local/pgsql/bin/
clusterdb data ecpg pg_basebackup pg_controldata pg_dumpall pg_recvlogical pg_rewind pg_upgrade postgres reindexdb
createdb dropdb initdb pgbench pg_ctl pg_isready pg_resetwal pg_test_fsync pg_verify_checksums postmaster vacuumdb
createuser dropuser pg_archivecleanup pg_config pg_dump pg_receivewal pg_restore pg_test_timing pg_waldump psql
faruukh#ubuntu:~$ export PATH=/usr/local/pgsql/bin//psql:$PATH
faruukh#ubuntu:~$ export PGDATA=/usr/local/pgsql/bin//psql/data
faruukh#ubuntu:~$ sudo chown -R faruukh /usr/local/pgsql/
faruukh#ubuntu:~$ initdb
The files belonging to this database system will be owned by user "faruukh".
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.
initdb: could not access directory "/usr/local/pgsql/bin/psql/data": Not a directory
faruukh#ubuntu:~$ sudo initdb
sudo: initdb: command not found
faruukh#ubuntu:~$ export PGDATA=/usr/local/pgsql/bin//psql/data
faruukh#ubuntu:~$
faruukh#ubuntu:~$
faruukh#ubuntu:~$
faruukh#ubuntu:~$
faruukh#ubuntu:~$
faruukh#ubuntu:~$ which psql
/usr/local/pgsql/bin//psql
faruukh#ubuntu:~$ pg_ctl
pg_ctl pg_ctlcluster
faruukh#ubuntu:~$ pg_ctl
pg_ctl pg_ctlcluster
faruukh#ubuntu:~$ pg_ctl
pg_ctl pg_ctlcluster
faruukh#ubuntu:~$ pg_ctl start log -l
pg_ctl: option requires an argument -- 'l'
Try "pg_ctl --help" for more information.
faruukh#ubuntu:~$ pg_ctl
pg_ctl: no operation specified
Try "pg_ctl --help" for more information.
faruukh#ubuntu:~$
`
```
```[error][1]
Tried following commands
cd postgresql-12.0
which psql
ls /usr/local/pgsql/bin/
export PATH=/usr/local/pgsql/bin/:$PATH
export PGDATA=/usr/local/pgsql/bin/data
sudo chown -R faruukh /usr/local/pgsql/
Try:
su -l <username> to switch user account and then run initdb -D <path to where new db cluster should be stored>
For me which psql gives /usr/bin/psql.
To start postgres SQL: sudo systemctl start postgresql#12-main (if pg version 12 is installed)
To check if postgres is running: service postgresql status
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
Active: active (exited) since Sat 2023-02-18 23:01:05 IST; 7min ago
To get into postgres shell: sudo su - postgres, and type the password.
To get into psql shell: psql -p <port>.
This worked for me, try the following commads :
su --login faruukh
cd /usr/local/pgsql/bin/
./initdb -D /usr/local/pgsql/bin/psql/data
Here's the reference for initdb
I reproduced the error that you encountered:-
~/Downloads$ initdb
The files belonging to this database system will be owned by user "capnspek".
This user must also own the server process.
The database cluster will be initialized with locale "en_IN".
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/pgsql/bin/psql/data": Not a directory
Adding a ./data at the end of the command successfully initializes the database cluster.
~/Downloads$ initdb ./data
The files belonging to this database system will be owned by user "capnspek".
This user must also own the server process.
The database cluster will be initialized with locale "en_IN".
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 ./data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Kolkata
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
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 ./data -l logfile start
Hopefully, that solves your issue.
Read more about initdb here.
Follow the documentation from here.

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?

Cant connect to PostgreSQL database

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.

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.

postgres server started but could not connect to server

After I lost all the data on my computer (had to have everything erased by the Apple store), I installed postgres on my Mac using Homebrew with the command brew install postgres. I then started the server (with confirmation that the server was started) and tried to create a database for a Sinatra project that uses postgres, using the command
createdb db_development
However, I got the following error:
createdb: could not connect to database postgres: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
After searching online, I discovered that I might have to run the following initdb command
initdb /usr/local/var/postgres
but when I did it, I got this message
initdb /usr/local/var/postgres
The files belonging to this database system will be owned by user "braindead".
This user must also own the server process.
The database cluster will be initialized with locale "en_CA.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/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
So, why might postgres have confirmed the server had started at the same time I got this error could not connect to database postgres: could not connect to server: No such file or directory, and how could I fix this?
the READ.me for the project said
bundle install
createdb db_development
rake db:migrate
It either tries to connect to the postgres that comes with MacOS or PG environment variables are not set.
Try:
createdb -h localhost db_development
these are postgres environment variables for postgres. Also PostgresApp is a very convenient way to use Postgresql on Mac
Postgres has a "unix_socket_directory" setting located in postgresql.conf
Please verify that this setting exists and that the directory itself exists, then restart your server. If it doesn't exist, set it to something like /var/run (or another directory that exists) and restart the server.