R_Moose
R_Moose

Reputation: 105

Configuration issue Postgres on Ubuntu?

I have installed Postgres 12 on Ubuntu by building it from source and I am facing two issues:

  1. Although I followed the installation manual from Postgrez, every time I restart my computer, my Postgres server stopz and is no longer seen as a running process.

To start it the first time after install, I do this from the terminal:

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

After a restart, to start DB again when I run: /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data, it throws this error:

initdb: error: 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".

Does that mean that every time I start Postgres after a restart, I have to create a new /data directory?

  1. Upon installing Postgres sing pip or pip3, one can just switch user to postgres and run psql to enter postgres, however now I have to run "/usr/local/bin/psql". Please note I have exported all the paths per https://www.postgresql.org/docs/12/installation.html. How can I fix this? Can an alias be set for this?

Upvotes: 0

Views: 273

Answers (3)

jjanes
jjanes

Reputation: 44137

After a restart, to start DB again when I run: /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data, it throws this error:

Does that mean that every time I start Postgres after a restart, I have to create a new /data directory?

No, quite the opposite. You don't need to initdb after the first time, you just need to start. It is your attempt to initdb when you don't need to which is causing the error message. Note that attempting to initdb isn't doing any harm, because it refused to run. It just generates log/console noise.

Upon installing Postgres sing pip or pip3, one can just switch user to postgres and run psql to enter postgres, however now I have to run "/usr/local/bin/psql". Please note I have exported all the paths per https://www.postgresql.org/docs/12/installation.html. How can I fix this?

I don't know what your first sentence means, as you don't use pip or pip3 to install PostgreSQL (or at least, the docs don't describe doing so) although you might use them to install psycopg2 to enable python to talk to PostgreSQL.

You could use an alias, but it would probably make more sense to edit ~/.bash_profile to set the PATH, as described from the page you linked to under Environment Variables.

Upvotes: 1

R_Moose
R_Moose

Reputation: 105

  1. /usr/local/pgsql/bin/pg_ctl start -D '/usr/local/pgsql/data'

  2. Export following in postgres user account's ~/.bashrc:

    LD_LIBRARY_PATH=/usr/local/pgsql/lib export LD_LIBRARY_PATH PATH=/usr/local/pgsql/bin:$PATH export PATH

Upvotes: 0

Dan
Dan

Reputation: 1881

You have to register postgreSQL as a service.

run this:

pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]

Example:

pg_ctl register -N postgresql -U OS_username -P OS_password -D '/etc/postgresql/12/data' -w

More info in the manual: pg_ctl

Notes:

  • Username and Password is related to the OS, not postgresql
  • If you have doubts read the manual.

Upvotes: 0

Related Questions