postgres server started but could not connect to server - postgresql

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.

Related

Saving PostgreSQL database to an external HDD

I need to save a database to an external HDD. This question has been asked previously here. However, upon changing the data_directory variable in the postgresql.conf file that exists in /usr/local/var/postgres/ to
data_directory = /path/to/externalHDD/directory/
I then restart the PostgreSQL server with brew services restart postgresql, but when I try to reconnect to the database via
psql -U username -d postgres
I receive the following error:
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
How do I fix this?
If all you did was point data_directory somewhere else, that cannot work, because there is no data directory at the destination. You have to create the data directory using initdb.

Distribution, Installation, and Connectivity of PostgreSQL

Have not been able to find an answer to this yet, but if this a duplicate, please mark accordingly!
Trying to understand how PostgreSQL can be distributed to an end-user via silent installation and the respective actions if:
PostgreSQL is already installed on the computer system
How to connect to PostgreSQL as a superuser, if it is already installed
Create a database (relating to #2, since we would not know the credentials of postgresql user when PostgreSQL was initially installed)
For #1, from my research and understanding, there are two methods:
Determining if a postgresql-[version] service is installed (per this QA)
Determining if HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\ registry exists, and if it does, seeing the versions and service names under the \Installations\ and \Services\ subs
However, I am more concerned about the connectivity part. If my application (to be written in C#) is dependent on a database name "MyProgram," how would it be possible to create a database in PostgreSQL and with what credentials?
From my understanding, the way to go would be to attempt to log in as the default superuser, postgres, to the default database, postgres, and create a new user and database from that connection. But, the password for postgres user is set during by the user/program that is initially is installing PostgreSQL.
How to go about this?
Any help is much appreciated!
IMPORTANT NOTE: This may not be the most ethical/proper way of doing this, but it appears to get the job done.
After numerous hours of digging, using the registry key on Windows is the best methodology, it seems, to determine if the version of PostgreSQL you intend to install, if it is already installed.
Assuming that I am running on the Windows system with Administrative rights, in theory, I should be able to change the login credential requirements of the pg_hba.conf file in the data directory (i.e. cluster) that already exists to allow myself to create the database(s) and user(s) I need to, before reverting the credential requirement settings to what they originally were.
So, the answer I have come to the conclusion with is:
Determine if PostgreSQL is already installed or not. Look at the HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installation\[version] registry, where [version] is formatted as postgresql-[32 or 64 bit]-[PostgreSQL version], e.g. postgresql-x64-12). If the registry exists, then data should exist for the Data Directory value... obtain that data, and that's where the cluster is located.
Step [2] and on are obviously for when PostgreSQL is already installed.
Make a copy of the pg_hba.conf file in the cluster directory provided by the Data Directory value from step [1].
This will be the file we restore to after we are done. Save file to a temporary directory, such as Desktop.
In the pg_hba.conf file in the cluster directory, change all connection types' methods to trust
Example:
# TYPE DATABASE USER ADDRESS METHOD
host all all 127.0.0.1/32 trust
Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory>
NOTE: pg_ctl.exe is located under the \bin\ folder of Postgres' installation directory.
Example: C:\Program Files\PostgreSQL\12\bin\pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\12\data\"
Connect to the cluster and issue the command to create a superuser role for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE ROLE <role name> LOGIN SUPERUSER PASSWORD '<password>';
In the above command, I have the cluster running on the local computer (i.e. localhost, IP address 127.0.0.1) on port # 5432 (default), connecting to the default database postgres and issuing the command to create a role with whatever role name provided in place of <role name>, with SUPERUSER rights and the password provided in place of <password>.
Since one HAS to connect to a database, I am connecting to the default one postgres, otherwise template0 and template1 are default databases that could also be utilized.
Connect to the cluster and issue the command to create the needed database for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE DATABASE <database name>;
Replace the pg_hba.conf file with the original
Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory>

How to add Postgres on Mac OS with MAMP to PATH?

I'm following this tutorial on how to run Postgres on MAC OS X with MAMP. But I can't seem to add PostgreSQL to my PATH variable. When I run export PATH="/Library/PostgreSQL/11/bin" I get this error:
prompt_context:13: command not found: whoami
prompt_background_jobs:1: command not found: wc
prompt_background_jobs:2: command not found: awk
Here's exactly what I did:
I downloaded PostgreSQL 11.2 from Enterprise DB
I installed it using the installer:
username: postgres
password: postgres
port: 5433
The PostgreSQL folder can now be found here: /Library/PostgreSQL. Inside there's a folder called 11. Inside /Library/PostgreSQL/11 I can see the bin folder and data folder.
There's another folder inside my Applications folder called PostgreSQL 11. Here I can see a Documentation folder and some other programs I believe:
Application Stack Builder
pgAdmin 4
Reload Configuration
SQL Shell (psql)
The following error appears when I ignore setting the PATH variable and run su postgres:
su: Sorry
or sudo postgres:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server
Also, running psql gives me this error, because PostgreSQL is not included in my environmental variables:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Note: this error asks if the server is accepting Unix domain socket with a port 5432, while I installed PostgreSQL on port 5433, what's up with that? Moving to /Library/PostgreSQL/11/bin with cd and running sudo ./psql (should run the Unix Executable file psql) gives me the same error.
Resources that didn't help me any further:
Createuser: could not connect to database postgres: FATAL: role "tom" does not exist
https://dba.stackexchange.com/questions/3005/how-to-run-psql-on-mac-os-x
Psql could not connect to server: No such file or directory, 5432 error?
When you run export PATH="/Library/PostgreSQL/11/bin" you are telling the system that all the commands you need are in that single directory. Since you also need standard system utilities like wc you also need to include the directories where they reside in your PATH. Normally you would do this to simply add the postgres directories to the list of directories searched for commands:
export PATH="/Library/PostgreSQL/11/bin:$PATH"

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.

Heroku iOS Tutorial using Postgres

I am following the tutorial on Heroku on how to use heroku on iOS. The example uses a postgres database to store images and geolocation. I downloaded the postgres app on my Lion OSX and it says the postgres server running correctly. Now I am stuck on this step:
Update the config/database.yml file to reference the correct database user and password for >the development and test environments. Then create the local development database and run the >migrations.
$ bundle exec rake db:create db:migrate
== CreatePhotos: migrating ===========
-- create_table(:photos)
...
But I receive the following error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
....
What did I do wrong? I tried to manually create the postgres database using the createdb command but I go the same connectivity error.
This error occurs when PostgreSQL is not listening on the socket specified. Causes can include (in order to check)
PostgreSQL not properly installed
PostgreSQL stopped or not running.
PostgreSQL listening on a different socket or in a different directory. This information is specified in the postgresql.conf.